ip地址做校验

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")));
            }
            
        }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值