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

1.邮箱校验规则

电子邮件地址由由字母、数字、下划线或短横线组成的用户名部分,后跟@符号,然后是一个或多个由字母和数字组成的域名部分,最后以.com、.cn、.net或.org为结尾

javascript 复制代码
//邮箱校验
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.邮箱校验规则
javascript 复制代码
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校验规则
javascript 复制代码
//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.身份证号码校验规则
javascript 复制代码
//身份证号校验
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.微信校验规则
javascript 复制代码
//微信校验
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.电话校验规则
javascript 复制代码
//电话校验
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.银行卡号校验规则
javascript 复制代码
//银行卡号校验
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.数字字母校验规则
javascript 复制代码
//数字字母校验
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.整数校验规则
javascript 复制代码
//整数校验
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.数字校验规则
javascript 复制代码
//数字校验
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.文本校验规则
javascript 复制代码
//文本校验
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();
  }
}
最后附上rules中的使用方法
java 复制代码
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'
        }]
 
}
相关推荐
煤泥做不到的!8 分钟前
挑战一个月基本掌握C++(第十一天)进阶文件,异常处理,动态内存
开发语言·c++
F-2H11 分钟前
C语言:指针4(常量指针和指针常量及动态内存分配)
java·linux·c语言·开发语言·前端·c++
bryant_meng1 小时前
【python】OpenCV—Image Moments
开发语言·python·opencv·moments·图片矩
若亦_Royi2 小时前
C++ 的大括号的用法合集
开发语言·c++
资源补给站3 小时前
大恒相机开发(2)—Python软触发调用采集图像
开发语言·python·数码相机
m0_748247553 小时前
Web 应用项目开发全流程解析与实战经验分享
开发语言·前端·php
6.943 小时前
Scala学习记录 递归调用 练习
开发语言·学习·scala
FF在路上4 小时前
Knife4j调试实体类传参扁平化模式修改:default-flat-param-object: true
java·开发语言
真的很上进4 小时前
如何借助 Babel+TS+ESLint 构建现代 JS 工程环境?
java·前端·javascript·css·react.js·vue·html
众拾达人4 小时前
Android自动化测试实战 Java篇 主流工具 框架 脚本
android·java·开发语言