ip校验:
// ip校验
verificationIp(val) {
const RE = /^(\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])$/;
return RE.test(val);
},
validatorIP(rule, val, cb){
const res = this.verificationIp(val);
if (res) {
cb();
} else {
cb(new Error(window.$I18n.t("cn.ipcheck.tip.ip")));
}
},
调用:
子网掩码校验:
// 子网掩码校验
checkSubnetMask(rule, val, cb) {
let subnetMask = val;
const RE = /^((254|252|248|240|224|192|128)\.0\.0\.0|255\.(254|252|248|240|224|192|128|0)\.0\.0|255\.255\.(254|252|248|240|224|192|128|0)\.0|255\.255\.255\.(254|252|248|240|224|192|128|0))$/;
if (RE.test(subnetMask)) {
let IPArray = subnetMask.split(".");
let ip1 = parseInt(IPArray[0]);
let ip2 = parseInt(IPArray[1]);
let ip3 = parseInt(IPArray[2]);
let ip4 = parseInt(IPArray[3]);
if (ip1 < 0 || ip1 > 255 || (ip2 < 0 || ip2 > 255) || (ip3 < 0 || ip3 > 255) || (ip4 < 0 || ip4 > 255)) {
cb(new Error(window.$I18n.t("cn.ipcheck.tip.subnetmask")));
return;
}
let ipbinary = this.checkIputfomartIP(ip1) + this.checkIputfomartIP(ip2) + this.checkIputfomartIP(ip3) + this.checkIputfomartIP(ip4);
if (-1 != ipbinary.indexOf("01")) {
cb(new Error(window.$I18n.t("cn.ipcheck.tip.subnetmask")));
return;
}
cb();
} else {
cb(new Error(window.$I18n.t("cn.ipcheck.tip.subnetmask")));
}
},
checkIputfomartIP(ip) {
return (ip + 256).toString(2).substring(1); // 格式化输出(补零)
},
// 检查网关与ip、子网的关系
validateGatewayAddress(val, ip, subnetMask, gateway, callback) {
const valid = this.verificationIp(val);
// 判断ip和网关是否一样
if(ip.toString() !== gateway.toString()) {
if (valid) {
let res0 = parseInt(ip[0]) & parseInt(subnetMask[0]);
let res1 = parseInt(ip[1]) & parseInt(subnetMask[1]);
let res2 = parseInt(ip[2]) & parseInt(subnetMask[2]);
let res3 = parseInt(ip[3]) & parseInt(subnetMask[3]);
let res0gw = parseInt(gateway[0]) & parseInt(subnetMask[0]);
let res1gw = parseInt(gateway[1]) & parseInt(subnetMask[1]);
let res2gw = parseInt(gateway[2]) & parseInt(subnetMask[2]);
let res3gw = parseInt(gateway[3]) & parseInt(subnetMask[3]);
if (res0 == res0gw && res1 == res1gw && res2 == res2gw && res3 == res3gw) {
callback();
} else {
callback(new Error(window.$I18n.t("cn.ipcheck.tip.gateway"))); // IP地址与子网掩码、网关地址不匹配
}
} else {
callback(new Error(window.$I18n.t("cn.ipcheck.tip.gatewayValidator")));
}
}else {
callback(new Error(window.$I18n.t("cn.ipcheck.tip.gateway")));
}
}