Vue+elementUI表单正则校验
Vue+elementUI表单正则校验数字校验var checkNumber = (rule, value, callback) => {if (value && value !== ''){let regEn = /^(\-|\+)?\d+(\.\d)?$/if (!regEn.test(value)) {callback(...
·
Vue+elementUI表单正则校验
- 数字校验
var checkNumber = (rule, value, callback) => { if (value && value !== ''){ let regEn = /^(\-|\+)?\d+(\.\d)?$/ if (!regEn.test(value)) { callback(new Error('请输入数字')) } else { callback() } } else { callback() } }
2、正整数(常用于年龄数量类的校验)
var checkNumber = (rule, value, callback) => { let regEn = /^[1-9]\d*$/ if (!regEn.test(value)) { callback(new Error('正整数')) } else { callback() } }
3、身份证校验(比较详细)
var checkIdCard = (rule, value, callback) => { let 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() } } } } }
4、车牌号校验-包含新能源
var 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() } }
5、手机号和座机号
checkPhone: (rule, value, callback) => { let regEn = /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/ if (!regEn.test(value)) { callback(new Error('请输入正确号码')) } else { callback() } }
更多推荐
已为社区贡献18条内容
所有评论(0)