validator.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. isValidFloat: function(value) {
  28. if (typeof value === 'string' && value.trim() === '') {
  29. return false;
  30. }
  31. var floatRegex = /^-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/;
  32. return floatRegex.test(value);
  33. },
  34. /**
  35. * validate directory with slash at the start
  36. * @param value
  37. * @return {Boolean}
  38. */
  39. isValidDir: function(value){
  40. var floatRegex = /^\/[0-9a-z]*/;
  41. var dirs = value.replace(/,/g,' ').trim().split(new RegExp("\\s+", "g"));
  42. for(var i = 0; i < dirs.length; i++){
  43. if(!floatRegex.test(dirs[i])){
  44. return false;
  45. }
  46. }
  47. return true;
  48. },
  49. /**
  50. * validate ip address with port
  51. * @param value
  52. * @return {Boolean}
  53. */
  54. isIpAddress: function(value) {
  55. var ipRegex = /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(\:[0-9]{1,5})?$/;
  56. return ipRegex.test(value);
  57. },
  58. /**
  59. * validate hostname
  60. * @param value
  61. * @return {Boolean}
  62. */
  63. isHostname: function(value) {
  64. var regex = /^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$/;
  65. return regex.test(value);
  66. },
  67. /**
  68. * validate domain name with port
  69. * @param value
  70. * @return {Boolean}
  71. */
  72. isDomainName: function(value) {
  73. var domainRegex = /^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$/;
  74. return domainRegex.test(value);
  75. },
  76. /**
  77. * validate username
  78. * @param value
  79. * @return {Boolean}
  80. */
  81. isValidUserName: function(value) {
  82. var usernameRegex = /^[a-z]([-a-z0-9]{0,30})\$?$/;
  83. return usernameRegex.test(value);
  84. },
  85. empty:function (e) {
  86. switch (e) {
  87. case "":
  88. case 0:
  89. case "0":
  90. case null:
  91. case false:
  92. case typeof this == "undefined":
  93. return true;
  94. default :
  95. return false;
  96. }
  97. }
  98. };