其他分享
首页 > 其他分享> > 验证规则汇总

验证规则汇总

作者:互联网

// 正则匹配字符串
const regExpConst = {
ipRegex: /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/,
// eslint-disable-next-line no-useless-escape
macRegex: /^[A-Fa-f0-9]{1,2}[A-Fa-f0-9]{1,2}\-[A-Fa-f0-9]{1,2}[A-Fa-f0-9]{1,2}\-[A-Fa-f0-9]{1,2}[A-Fa-f0-9]{1,2}$/
}
// ip正则验证
let validIp = (rule, value, callback) => {
const reg = regExpConst.ipRegex
if (reg.test(value)) {
callback()
} else {
return callback(new Error('IP输入格式不合法!'))
}
}
// ip正则验证, 只允许a,b,c类地址
let validIpABC = (rule, value, callback) => {
if (value === undefined || value === '') {
callback()
} else {
let tmpValue = util.getIntValue(value.substr(0, value.indexOf('.')))
if (tmpValue < 224) {
callback()
} else {
return callback(new Error('非A、B、C类IP地址!'))
}
}
}


let validMac = (rule, value, callback) => {
// mac地址格式校验:x-x-x(支持未输入位自动补0)
let tmpArray = []
value.split('-').forEach(
function (item) {
const len = item.length
let tmpValue = item
switch (len) {
case 1:
tmpValue = '000' + tmpValue
break
case 2:
tmpValue = '00' + tmpValue
break
case 3:
tmpValue = '0' + tmpValue
break
default:
break
}
tmpArray.push(tmpValue)
}
)
if (tmpArray.length !== 3) {
return callback(new Error('Mac输入格式不合法!'))
}
value = tmpArray[0] + '-' + tmpArray[1] + '-' + tmpArray[2]
const reg = regExpConst.macRegex
if (reg.test(value)) {
callback()
} else {
return callback(new Error('Mac输入格式不合法!'))
}
}

标签:f0,验证,汇总,Fa,value,callback,let,规则,tmpValue
来源: https://www.cnblogs.com/wsb20200618/p/14957842.html