1,手机号校验
const phonrReg = /^ [1][3,4,5,6,7,8,9][0-9]{9}$/
2, 身份证验证
const sfzReg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
3,邮箱验证
const emailReg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/
4,URL校验
const urlReg = /^((https?|ftp|file):\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/
5,IPv4的校验
const ipv4Reg = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
6,16进制颜色的校验
const color16Reg = /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/
7,日期 YYYY-MM-DD
const dateReg = /^\d{4}(\-)\d{1,2}\1\d{1,2}$/
8,日期 YYYY-MM-DD hh:mm:ss
const dateReg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/
9,整数的校验
const intReg = /^[-+]?\d*$/
10,小数的校验
const floatReg = /^[-\+]?\d+(\.\d+)?$/
11,保留n位小数
function checkFloat(n) {
return new RegExp(`^([1-9]+[\d]*(.[0-9]{1,${n}})?)$`)
}
12,字母校验
const letterReg = /^[a-zA-Z]+$/
13,中文
const cnReg = /[\u4E00-\u9FA5]/
14,文件拓展名
function checkFileName (arr) {
arr = arr.map(name => `.${name}`).join('|')
return new RegExp(`(${arr})$`)
}
const filenameReg = checkFileName(['jpg', 'png', 'txt'])