- 手机号校验
const validatePhone = (rule, value, callback) => {
if (value === '') {
callback(new Error('请输入手机号码~'))
} {
const phoneRegex = /^1[34578]\d{9}$/
if (!phoneRegex.test(value)) {
callback(new Error('手机号码格式不正确!'))
} else {
callback()
}
}
}
- 邮箱校验
const validateEmail = (rule, value, callback) => {
if (value === '') {
callback(new Error('请输入邮箱~'))
return
}
const emailRegex = /^(([^<>()\[\]\\.,;:\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 (!emailRegex.test(value)) {
callback(new Error('邮箱格式不正确!'))
} else {
callback()
}
}
- 身份证校验
const checkIdCard = (rule, value, callback) => {
const regEn = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
if (!regEn.test(value)) {
callback(new Error('请输入正确身份证'))
} else {
// 校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10
// 下面分别分析出生日期和校验位
let num = value.toUpperCase()
let len, re
len = num.length
if (len === 15) {
re = new RegExp(/^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/)
let arrSplit = num.match(re) // 检查生日日期是否正确
let dtmBirth = new Date('19' + arrSplit[2] + '/' + arrSplit[3] + '/' + arrSplit[4])
let bGoodDay
bGoodDay = (dtmBirth.getYear() === Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) === Number(arrSplit[3])) && (dtmBirth.getDate() === Number(arrSplit[4]))
if (!bGoodDay) {
callback(new Error('请输入正确身份证'))
} else { // 将15位身份证转成18位 //校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10
let arrInt = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
let arrCh = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
var nTemp = 0, i
num = num.substr(0, 6) + '19' + num.substr(6, num.length - 6)
for (i = 0; i < 17; i++) {
nTemp += num.substr(i, 1) * arrInt[i]
}
num += arrCh[nTemp % 11]
callback()
}
}
if (len === 18) {
re = new RegExp(/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/)
let arrSplit = num.match(re) // 检查生日日期是否正确
let dtmBirth = new Date(arrSplit[2] + '/' + arrSplit[3] + '/' + arrSplit[4])
let bGoodDay
bGoodDay = (dtmBirth.getFullYear() === Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) === Number(arrSplit[3])) && (dtmBirth.getDate() === Number(arrSplit[4]))
if (!bGoodDay) {
callback(new Error('请输入正确身份证'))
} else { // 检验18位身份证的校验码是否正确。 //校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10
let valnum
let arrInt = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
let arrCh = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
var nTemp = 0, i
for (i = 0; i < 17; i++) {
nTemp += num.substr(i, 1) * arrInt[i]
}
valnum = arrCh[nTemp % 11]
if (valnum !== num.substr(17, 1)) {
callback(new Error('请输入正确身份证'))
} else {
callback()
}
}
}
}
}
- 车牌号校验
const checkPlateNum = (rule, value, callback) => {
if (value && value !== '') {
let regEn = /(^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$)/
if (!regEn.test(value)) {
callback(new Error('请输入正确车牌号'))
} else {
callback()
}
} else {
callback()
}
}
- 数字校验
const checkNumber = (rule, value, callback) => {
if (value && value !== ''){
const regEn = /^(\-|\+)?\d+(\.\d)?$/
if (!regEn.test(value)) {
callback(new Error('请输入数字'))
} else {
callback()
}
} else {
callback()
}
}
- 正整数校验
const checkNumber = (rule, value, callback) => {
const regEn = /^[1-9]\d*$/
if (!regEn.test(value)) {
callback(new Error('正整数'))
} else {
callback()
}
- 密码校验
const validatePass = (rule, value, callback) => {
if (value === '') {
callback(new Error('请设置正确格式的密码'))
} else if(value.length < 6){
callback(new Error('密码长度最小6位'))
}else{
callback()
}
}
- url 校验
const validatePhone = (rule, value, callback) => {
if (value === '') {
callback(new Error('请输入url ~'))
} {
const urlregex = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
if (!urlregex.test(value)) {
callback(new Error('url格式不正确!'))
} else {
callback()
}
}
}