0

在我的项目中,我能够像这样验证 IP 地址:

function (value, element) {
  var octet = '(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])';
  var ip = '(?:' + octet + '\\.){3}' + octet;
  var ipRE = new RegExp('^' + ip + '$');
  return (ipRE.test(value));
}

目前,它接受单个 IPv4 版本地址。我也想接受多种 IPv4/IPv6 版本格式。

例子:

'0:0:0:0:0:ffff:192.1.56.10,172.30.60.1,fe80:41b2:41b2:935c:b113:da1a' 

我正在使用 jQuery.validator API 来执行验证。

请提供建议。

4

4 回答 4

1

用逗号分割输入会更简单,然后解析每个部分:找出地址是 IPv4 还是 IPv6,然后使用正确的正则表达式。

于 2012-11-28T09:35:06.770 回答
0

我发现这个正则表达式有时对 IP 地址验证很有用

(?2[0-4]\d|25[0-5]|[01]?\d\d?).(?2[0-4]\d|25[0-5]|[01] ?\d\d?).(?2[0-4]\d|25[0-5]|[01]?\d\d?).(?2[0-4]\d|25[ 0-5]|[01]?\d\d?)

希望有帮助

于 2012-11-28T09:24:18.450 回答
0

编辑:在同一功能中适用于 v4 和 v6。缺点:依赖PHP5。

<?php
function checkIP($IPaddr) {
     $packetNot = inet_pton($IPaddr);
     if ($packetNot === FALSE) return false;
     if (inet_ntop($packetNot) === FALSE) return false;
     return true;
} ?>
于 2012-11-28T09:34:26.850 回答
0

以下代码用于在 jquery 中同时接受 IPv6 和 IPv4 版本。

 function checkipv6(str) {
            var perlipv6regex = "^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$";
            var aeronipv6regex = "^\s*((?=.*::.*)(::)?([0-9A-F]{1,4}(:(?=[0-9A-F])|(?!\2)(?!\5)(::)|\z)){0,7}|((?=.*::.*)(::)?([0-9A-F]{1,4}(:(?=[0-9A-F])|(?!\7)(?!\10)(::))){0,5}|([0-9A-F]{1,4}:){6})((25[0-5]|(2[0-4]|1[0-9]|[1-9]?)[0-9])(\.(?=.)|\z)){4}|([0-9A-F]{1,4}:){7}[0-9A-F]{1,4})\s*$";

            var regex = "/" + perlipv6regex + "/";
            return (/^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/.test(str));
}



function (value, element) 
{

            var returnvalue;
            alert("IPcheck " + value);
            var result = value.split(",");

            for (i = 0; i < result.length; i++) {

                var octet = '(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])';

                if (result[i].split(":").length == 1) {
                    var ip = '(?:' + octet + '\\.){3}' + octet;
                    var ipRE = new RegExp('^' + ip + '$');
                    returnvalue = ipRE.test(result[i]);

                }
                else {
                    returnvalue = checkipv6(result[i]);
                }
                // var octet= '([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])'
                //var octet = '(?2[0-4]\d|25[0-5]|[01]?\d\d?).(?2[0-4]\d|25[0-5]|[01]?\d\d?).(?2[0-4]\d|25[0-5]|[01]?\d\d?).(?2[0-4]\d|25[0-5]|[01]?\d\d?)'
                //                var ip = '(?:' + octet + '\\.){3}' + octet;
                //                var ipRE = new RegExp('^' + ip + '$');
                if (returnvalue == false) {
                    return returnvalue;
                }
            }
            return returnvalue;
        },
        "<br/>Please enter a Valid IP Address"
    );
于 2012-12-04T06:49:27.553 回答