验证规则汇总

// 正则匹配字符串
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输入格式不合法!'))
}
}
上一篇:2011年下半年 网络管理员 下午试卷 案例 软考真题【含答案和解析】


下一篇:三层交换实现VLAN间通信