validator-rules.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. window.ValidatorRules = {
  2. // 非空验证
  3. 'notNull': {
  4. validate: (value) => {
  5. return value !== null && value !== undefined && value !== '';
  6. },
  7. message: '{field}不能为空'
  8. },
  9. // 邮箱验证
  10. 'email': {
  11. validate: (value) => {
  12. if (!value) return true; // 如果为空则跳过验证
  13. return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
  14. },
  15. message: '{field}格式不正确'
  16. },
  17. // 数字验证
  18. 'number': {
  19. validate: (value) => {
  20. if (!value) return true;
  21. return !isNaN(value) && typeof Number(value) === 'number';
  22. },
  23. message: '{field}必须是数字'
  24. },
  25. // 最小长度
  26. 'minLength': {
  27. validate: (value, min) => {
  28. if (!value) return true;
  29. return String(value).length >= min;
  30. },
  31. message: '{field}长度不能小于{min}个字符'
  32. },
  33. // 最大长度
  34. 'maxLength': {
  35. validate: (value, max) => {
  36. if (!value) return true;
  37. return String(value).length <= max;
  38. },
  39. message: '{field}长度不能超过{max}个字符'
  40. },
  41. // 证件号验证
  42. 'idCard': {
  43. validate: function(value, options) {
  44. // 如果有关联字段,按关联字段的值选择验证规则
  45. if (options.relField) {
  46. const typeElement = document.querySelector(`[name="${options.relField}"]`);
  47. const type = typeElement?.value;
  48. // 内置规则映射
  49. const rules = {
  50. '1': this.validateIdCard, // 居民身份证
  51. '6': this.validateHkIdCard, // 香港特区护照或身份证
  52. '7': this.validateMacauIdCard, // 澳门特区护照或身份证
  53. '8': this.validateTaiwanPass, // 台湾居民来往大陆通行证
  54. '10': this.validateForeignerCard // 外国人永久居留证
  55. };
  56. // 如果是空值或"请选择",则跳过验证
  57. if (!type || type === '') return true;
  58. const validator = rules[type];
  59. if (!validator) return true;
  60. return validator(value);
  61. }
  62. // 如果没有关联字段,默认使用身份证验证
  63. return this.validateIdCard(value);
  64. },
  65. message: '{field}格式不正确',
  66. // 居民身份证:18位数字,最后一位可能是X
  67. validateIdCard(value) {
  68. if (!value) return true;
  69. return /^[1-9]\d{5}(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dX]$/.test(value);
  70. },
  71. // 香港特区护照或身份证:字母开头+6位数字+(数字或A)
  72. validateHkIdCard(value) {
  73. if (!value) return true;
  74. // 支持两种格式:
  75. // 1. 香港身份证:1-2个字母 + 6位数字 + (数字或A)
  76. // 2. 香港特区护照:H + 8位数字
  77. return /^[A-Z]{1,2}[0-9]{6}\([0-9A]\)$/.test(value) || /^H[0-9]{8}$/.test(value);
  78. },
  79. // 澳门特区护照或身份证:1/5/7 + 6位数字 + (校验位)
  80. validateMacauIdCard(value) {
  81. if (!value) return true;
  82. // 支持两种格式:
  83. // 1. 澳门身份证:1/5/7 + 6位数字 + (数字)
  84. // 2. 澳门特区护照:MA + 8位数字
  85. return /^[157][0-9]{6}\([0-9]\)$/.test(value) || /^MA[0-9]{8}$/.test(value);
  86. },
  87. // 台湾居民来往大陆通行证:8位数字
  88. validateTaiwanPass(value) {
  89. if (!value) return true;
  90. return /^[0-9]{8}$/.test(value);
  91. },
  92. validateForeignerCard(value) {
  93. if (!value) return true;
  94. return /^[A-Z]{3}\d{6}$/.test(value);
  95. }
  96. },
  97. 'custom': {
  98. validate: function(value, options) {
  99. // 如果没有自定义验证函数,直接返回 true
  100. if (!options.validate) return true;
  101. // 获取关联字段的值(如果有)
  102. let relValue;
  103. if (options.relField) {
  104. const relElement = document.querySelector(`[name="${options.relField}"]`);
  105. relValue = relElement?.value;
  106. }
  107. // 执行自定义验证
  108. const result = options.validate(value, relValue);
  109. // 如果返回的是对象,设置自定义错误消息
  110. if (result && typeof result === 'object') {
  111. this.message = result.message;
  112. return result.valid;
  113. }
  114. // 如果返回的是 boolean,使用默认错误消息
  115. return result;
  116. },
  117. message: '{field}验证失败'
  118. }
  119. };