validator.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. module.exports = {
  19. isValidEmail: function(value) {
  20. var emailRegex = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i;
  21. return emailRegex.test(value);
  22. },
  23. isValidInt: function(value) {
  24. var intRegex = /^-?\d+$/;
  25. return intRegex.test(value);
  26. },
  27. isValidUNIXUser: function(value){
  28. var regex = /^[a-z_][a-z0-9_-]{0,31}$/;
  29. return regex.test(value);
  30. },
  31. isValidFloat: function(value) {
  32. if (typeof value === 'string' && value.trim() === '') {
  33. return false;
  34. }
  35. var floatRegex = /^-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/;
  36. return floatRegex.test(value);
  37. },
  38. /**
  39. * validate directory with slash at the start
  40. * @param value
  41. * @return {Boolean}
  42. */
  43. isValidDir: function(value){
  44. var floatRegex = /^\/[0-9a-z]*/;
  45. var dirs = value.replace(/,/g,' ').trim().split(new RegExp("\\s+", "g"));
  46. for(var i = 0; i < dirs.length; i++){
  47. if(!floatRegex.test(dirs[i])){
  48. return false;
  49. }
  50. }
  51. return true;
  52. },
  53. /**
  54. * validate ip address with port
  55. * @param value
  56. * @return {Boolean}
  57. */
  58. isIpAddress: function(value) {
  59. var ipRegex = /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(\:[0-9]{1,5})?$/;
  60. return ipRegex.test(value);
  61. },
  62. /**
  63. * validate hostname
  64. * @param value
  65. * @return {Boolean}
  66. */
  67. isHostname: function(value) {
  68. var regex = /(?=^.{3,254}$)(^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*(\.[a-zA-Z]{1,62})$)/;
  69. return regex.test(value);
  70. },
  71. hasSpaces: function(value) {
  72. var regex = /(\s+)/;
  73. return regex.test(value);
  74. },
  75. isNotTrimmed: function(value) {
  76. var regex = /(^\s+|\s+$)/;
  77. return regex.test(value);
  78. },
  79. /**
  80. * validate domain name with port
  81. * @param value
  82. * @return {Boolean}
  83. */
  84. isDomainName: function(value) {
  85. var domainRegex = /^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$/;
  86. return domainRegex.test(value);
  87. },
  88. /**
  89. * validate username
  90. * @param value
  91. * @return {Boolean}
  92. */
  93. isValidUserName: function(value) {
  94. var usernameRegex = /^[a-z]([-a-z0-9]{0,30})$/;
  95. return usernameRegex.test(value);
  96. },
  97. /**
  98. * validate password
  99. * @param value
  100. * #return {Boolean}
  101. */
  102. isValidPassword: function(value) {
  103. var passwordRegex = /^.{5,}$/;
  104. return passwordRegex.test(value);
  105. },
  106. /**
  107. * validate key of configurations
  108. * @param value
  109. * @return {Boolean}
  110. */
  111. isValidConfigKey: function(value) {
  112. var configKeyRegex = /^[0-9a-z_\-\.]+$/i;
  113. return configKeyRegex.test(value);
  114. },
  115. empty:function (e) {
  116. switch (e) {
  117. case "":
  118. case 0:
  119. case "0":
  120. case null:
  121. case false:
  122. case typeof this == "undefined":
  123. return true;
  124. default :
  125. return false;
  126. }
  127. }
  128. };