| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- window.ValidatorRules = {
- // 非空验证
- 'notNull': {
- validate: (value) => {
- return value !== null && value !== undefined && value !== '';
- },
- message: '{field}不能为空'
- },
-
- // 邮箱验证
- 'email': {
- validate: (value) => {
- if (!value) return true; // 如果为空则跳过验证
- return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
- },
- message: '{field}格式不正确'
- },
-
- // 数字验证
- 'number': {
- validate: (value) => {
- if (!value) return true;
- return !isNaN(value) && typeof Number(value) === 'number';
- },
- message: '{field}必须是数字'
- },
-
- // 最小长度
- 'minLength': {
- validate: (value, min) => {
- if (!value) return true;
- return String(value).length >= min;
- },
- message: '{field}长度不能小于{min}个字符'
- },
-
- // 最大长度
- 'maxLength': {
- validate: (value, max) => {
- if (!value) return true;
- return String(value).length <= max;
- },
- message: '{field}长度不能超过{max}个字符'
- },
-
- // 证件号验证
- 'idCard': {
- validate: function(value, options) {
- // 如果有关联字段,按关联字段的值选择验证规则
- if (options.relField) {
- const typeElement = document.querySelector(`[name="${options.relField}"]`);
- const type = typeElement?.value;
- // 内置规则映射
- const rules = {
- '1': this.validateIdCard, // 居民身份证
- '6': this.validateHkIdCard, // 香港特区护照或身份证
- '7': this.validateMacauIdCard, // 澳门特区护照或身份证
- '8': this.validateTaiwanPass, // 台湾居民来往大陆通行证
- '10': this.validateForeignerCard // 外国人永久居留证
- };
- // 如果是空值或"请选择",则跳过验证
- if (!type || type === '') return true;
- const validator = rules[type];
- if (!validator) return true;
- return validator(value);
- }
-
- // 如果没有关联字段,默认使用身份证验证
- return this.validateIdCard(value);
- },
- message: '{field}格式不正确',
- // 居民身份证:18位数字,最后一位可能是X
- validateIdCard(value) {
- if (!value) return true;
- 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);
- },
- // 香港特区护照或身份证:字母开头+6位数字+(数字或A)
- validateHkIdCard(value) {
- if (!value) return true;
- // 支持两种格式:
- // 1. 香港身份证:1-2个字母 + 6位数字 + (数字或A)
- // 2. 香港特区护照:H + 8位数字
- return /^[A-Z]{1,2}[0-9]{6}\([0-9A]\)$/.test(value) || /^H[0-9]{8}$/.test(value);
- },
- // 澳门特区护照或身份证:1/5/7 + 6位数字 + (校验位)
- validateMacauIdCard(value) {
- if (!value) return true;
- // 支持两种格式:
- // 1. 澳门身份证:1/5/7 + 6位数字 + (数字)
- // 2. 澳门特区护照:MA + 8位数字
- return /^[157][0-9]{6}\([0-9]\)$/.test(value) || /^MA[0-9]{8}$/.test(value);
- },
- // 台湾居民来往大陆通行证:8位数字
- validateTaiwanPass(value) {
- if (!value) return true;
- return /^[0-9]{8}$/.test(value);
- },
- validateForeignerCard(value) {
- if (!value) return true;
- return /^[A-Z]{3}\d{6}$/.test(value);
- }
- },
- 'custom': {
- validate: function(value, options) {
- // 如果没有自定义验证函数,直接返回 true
- if (!options.validate) return true;
- // 获取关联字段的值(如果有)
- let relValue;
- if (options.relField) {
- const relElement = document.querySelector(`[name="${options.relField}"]`);
- relValue = relElement?.value;
- }
- // 执行自定义验证
- const result = options.validate(value, relValue);
-
- // 如果返回的是对象,设置自定义错误消息
- if (result && typeof result === 'object') {
- this.message = result.message;
- return result.valid;
- }
- // 如果返回的是 boolean,使用默认错误消息
- return result;
- },
- message: '{field}验证失败'
- }
- };
|