常用的表单校验规则——邮箱/QQ/身份证号码/微信/电话/数字字母/整数/文本/密码等

1.邮箱校验规则1

//邮箱校验
export const validateEmail = async (RuleObject, value) => {
  // const reg = new RegExp(/^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+$/)
  const reg = new RegExp(/^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/)
  if (value) {
    if (!reg.test(value)) {
      return Promise.reject('请输入正确的邮箱');
    } else {
      return Promise.resolve();
    }
  } else {
    return Promise.resolve();
  }
}

2.邮箱校验规则2

export const validateEmail1 = async (RuleObject, value) => {
  const reg = new RegExp(/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/)
  if (value) {
    if (!reg.test(value)) {
      return Promise.reject('请输入正确的邮箱');
    } else {
      return Promise.resolve();
    }
  } else {
    return Promise.resolve();
  }
}

3.QQ校验规则

//qq校验
export const validateQQ = async (RuleObject, value) => {
  const reg = new RegExp(/^[1-9][0-9]{4,10}$/);
  if (value) {
    if (!reg.test(value)) {
      return Promise.reject('请输入正确的QQ号');
    } else {
      return Promise.resolve();
    }
  } else {
    return Promise.resolve();
  }
}

4.身份证号码校验规则

//身份证号校验
export const validateIdCard = async (RuleObject, value) => {
  const reg = new RegExp(/(^\d{8}(0\d|10|11|12)([0-2]\d|30|31)\d{3}$)|(^\d{6}(18|19|20)\d{2}(0[1-9]|10|11|12)([0-2]\d|30|31)\d{3}(\d|X|x)$)/);
  const reg1 = new RegExp(/^[A-Z]\d{7,11}$/)
  if (value) {
    if (!reg1.test(value)&&!reg.test(value)) {
      return Promise.reject('请输入正确的身份证号');
    } else {
      return Promise.resolve();
    }
  } else {
    return Promise.resolve();
  }
}

5.微信校验规则

//微信校验
export const validateCharts = async (RuleObject, value) => {
  const reg = new RegExp(/^[a-zA-Z][-_a-zA-Z0-9]{5,19}$/);
  if (value) {

    if (!reg.test(value)) {
      return Promise.reject('请输入正确的微信号');
    } else {
      return Promise.resolve();
    }
  } else {
    return Promise.resolve();
  }
}

6.电话校验规则

//电话校验
export const validatPhone = async (RuleObject, value) => {
  const reg = new RegExp(/^(?:(?:\+|00)86)?1[3-9]\d{9}$/);
  if (value) {

    if (!reg.test(value)) {
      return Promise.reject('请输入正确的手机号');
    } else {
      return Promise.resolve();
    }
  } else {
    return Promise.resolve();
  }
}

7.银行卡号校验规则

//银行卡号校验
export const validatBankCard = async (RuleObject, value) => {
  const reg = new RegExp(/^[1-9]\d{9,29}$/);
  if (value) {
    if (!reg.test(value)) {
      return Promise.reject('请输入正确的银行卡号');
    } else {
      return Promise.resolve();
    }
  } else {
    return Promise.resolve();
  }
}

8.数字字母校验规则

//数字字母校验
export const validatNumberLetter = async (RuleObject, value) => {
  const reg = new RegExp(/^([A-Z]|[a-z]|[\d])*$/);
  if (value) {
    if (!reg.test(value)) {
      return Promise.reject('请输入正确的工号');
    } else {
      return Promise.resolve();
    }
  } else {
    return Promise.resolve();
  }
}

9.整数校验规则

//整数校验
export const validateInitNumber = async (RuleObject, value, min, max) => {
  if (value !== '' && value !== null && value !== undefined) {
    if (!Number(value) && value !== 0) {
      return Promise.reject('请输入数字');
    } else if (!Number.isInteger(Number(value))) {
      return Promise.reject('请输入整数');
    } else {
      if (max) {
        value = Number(value);
        if (value > max || value < min) {
          return Promise.reject('请输入' + min + '到' + max + '之间的数值');
        } else {
          return Promise.resolve();
        }
      } else {
        return Promise.resolve();
      }
    }
  } else {
    return Promise.resolve();
  }
}

10.数字校验规则

//数字校验
export const validateNumber = async (RuleObject, value, min, max) => {
  if (value !== '' && value !== null && value !== undefined) {
    if (!Number(value) && value !== 0) {
      return Promise.reject('请输入数字');
    } else {
      if (max) {
        if (value > max || value < min) {
          return Promise.reject('请输入' + min + '到' + max + '之间的数值');
        } else {
          return Promise.resolve();
        }
      } else {
        return Promise.resolve();
      }
    }
  } else {
    return Promise.resolve();
  }
}

11.文本校验规则

//文本校验
export const validateInitTxt = async (RuleObject, value, min, max) => {
  if (value) {
    if (max) {
      if (value.length > max || value.length < min) {
        return Promise.reject('请输入' + min + '到' + max + '个字符');
      } else {
        return Promise.resolve();
      }
    } else {
      return Promise.resolve();
    }
  } else {
    return Promise.resolve();
  }
}

12.密码校验规则

//密码校验
export const validatePWD = async (RuleObject, value, min, max) => {
  if (value) {
    if (max) {
      if (value.length > max || value.length < min) {
        return Promise.reject('请输入' + min + '到' + max + '个字符');
      } else {
        return Promise.resolve();
      }
    } else {
      return Promise.resolve();
    }
  } else {
    return Promise.resolve();
  }
}

最后附上rules中的使用方法

import { validateEmail,validateInitTxt, validateQQ, validateIdCard, validatPhone,validateEmail1, validatBankCard, validateInitNumber,validateNumber,validateCharts,validatNumberLetter  } from './validateForm.js';
export default  {
    '姓名': [
        {
            required: true, message: '请输入名字'
        },
        {
            validator: async (RuleObject, value) => validateInitTxt(RuleObject, value, 1, 30), trigger: 'change'
        }
    ],
    '年龄': [
        {
            required: true, message: '请输入年龄'
        },
        {
            validator: async (RuleObject, value) => validateInitNumber(RuleObject, value, 16, 65), trigger: 'change'
        }
    ],
    '身高': [
        {
            required: true, message: '请输入身高'
        },
        {
            validator: async (RuleObject, value) => validateInitNumber(RuleObject, value, 140, 210), trigger: 'change'
        }
    ],
    '体重': [
        {
            required: true, message: '请输入体重'
        },
        {
            validator: async (RuleObject, value) => validateNumber(RuleObject, value, 35, 200), trigger: 'change'
        }
    ],
    '邮件': [
        {
            required: true, message: '请输入邮件'
        },
        {
            validator: validateEmail, trigger: 'change'
        }
    ],
    '邮箱': [
        {
            required: true, message: '请输入邮箱'
        },
        {
            validator: validateEmail1, trigger: 'change'
        }
    ],
    'QQ': [
        {
            required: true, message: '请输入QQ'
        },
        {
            validator: validateQQ, trigger: 'change'
        }
    ],
    '微信': [
        {
            required: true,message: '请输入微信'
        },

        {
            validator: validateCharts, trigger: 'change'
        }
    ],
    '身份证': [
        {
            required: true, message: '请输入身份证号'
        },
        {
            validator: validateIdCard, trigger: 'change'
        }
    ],
    '电话': [
        {
            required: true, message: '请输入联系电话'
        },
        {
            validator: validatPhone, trigger: 'change'
        }
    ],
    '银行卡': [
        {
            required: true,message: '请输入银行卡卡号'
        },
        {
            validator: validatBankCard, trigger: 'change'
        }
    ],
    '留言板': [
        {
            required: true,message: '请输入'
        },
        {
            validator: async (RuleObject, value) => validateInitTxt(RuleObject, value, 1, 300), trigger: 'change'
        }
    ],
    '输入框50': [
        {
            required: true,message: '请输入'
        },
        {
            validator: async (RuleObject, value) => validateInitTxt(RuleObject, value, 1, 50), trigger: 'change'
        }
    ],
    '输入框30': [
        {
            required: true,message: '请输入'
        },
        {
            validator: async (RuleObject, value) => validateInitTxt(RuleObject, value, 1, 30), trigger: 'change'
        }
    ],

    '下拉框': [
        {
            required: true, message: '请选择'
        }
    ],
    '整数': [
        {
            required: true,message: '请输入'
        },
        {
            validator: validateInitNumber, trigger: 'change'
        }],
    '组合':[
        {
            required: true,message: '请输入'
        },
        {
            validator: validatNumberLetter, trigger: 'change'
        },
    ],
    '浮点数': [
        {
            required: true,message: '请输入'
        },
        {
            validator: validateNumber, trigger: 'change'
        }]

}
相关推荐
Themberfue20 分钟前
Java 网络原理 ①-IO多路复用 || 自定义协议 || XML || JSON
xml·java·开发语言·网络·计算机网络·json
m0_6996595621 分钟前
DAY3 QT简易登陆界面优化
开发语言·qt·命令模式
励志成为大佬的小杨1 小时前
关键字初级学习
c语言·开发语言·算法
szpc16212 小时前
100V宽压输入反激隔离电源,适用于N道沟MOSFET或GaN或5V栅极驱动器,无需光耦合
c语言·开发语言·人工智能·单片机·嵌入式硬件·生成对抗网络·fpga开发
m0_748234712 小时前
Java-33 深入浅出 Spring - FactoryBean 和 BeanFactory BeanPostProcessor
java·开发语言·spring
知初~2 小时前
java相关学习文档或网站整理
java·开发语言·学习
我走过的路你也许在走2 小时前
python装饰器学习案例
开发语言·python·学习
a栋栋栋2 小时前
apifox
java·前端·javascript
我的运维人生2 小时前
Python在数据处理与分析中的高效应用:以金融数据为例
开发语言·python·金融·运维开发·技术共享
sukalot3 小时前
windows C#-显式接口实现
开发语言·windows·c#