jQuery IP地址验证失败

时间:2012-07-02 14:13:06

标签: jquery regex

下面是代码片段,我编写了代码以掩盖IP地址并使用jQuery对其进行验证。但是,我收到有效IP地址的错误消息(如222.222.222.222)。有人可以帮忙吗?

            //Validate the form
            $('#form').validate({
                rules: {
                    ip: {
                        required: true,
                        IP4Checker: true
                    },
                    subnet: {
                        required: true,
                        IP4Checker: true
                    },
                    gateway: {
                        required: true,
                        IP4Checker: true
                    },
                    dns1: {
                        required: true,
                        IP4Checker: true
                    },
                    dns2: {
                        required: true,
                        IP4Checker: true
                    }
                },
                messages: {
                    ip: "Please enter a valid IP Address",
                    subnet: "Please enter a valid Subnet Mask Address",
                    gateway: "Please enter a valid Gateway Address",
                    dns1: "Please enter a valid DNS1 Address",
                    dns2: "Please enter a valid DNS2 Address"
                }
            });

            //Validate the IP addresses
            $(function() {
                $.validator.addMethod('IP4Checker', function(value) {
                    var ip = "^(?:(?:25[0-5]2[0-4][0-9][01]?[0-9][0-9]?)\\.){3}" +
                        "(?:25[0-5]2[0-4][0-9][01]?[0-9][0-9]?)$";
                    return value.match(ip);
                });
            });

            //Set mask for IP address fields
            $(".ip").mask("999 . 999 . 999 . 999");

            //Store numbers in hidden field
            $(".ip").blur(function () {

                //Create char array from phone number field
                var charArray = $(this).val().split("");

                var num = "";

                //taking the input
                $.each(charArray, function(index, value) {
                    num = num + value;
                });
            });

2 个答案:

答案 0 :(得分:2)

你的正则表达式错过了它字符即。

\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)

另请注意,您可以将此缩短为

\b(?:\d{1,3}\.){3}\d{1,3}\b

如果您不需要检查它是0-255之间的有效IP地址

任何\都需要自行转义,因此它将是\

reference

<强>更新

OP显示小提琴并且没有读过上面的线也掩码在两侧都有空格。所以正则表达式永远不会有效,因为掩码添加了空格。

fixed Fiddle

答案 1 :(得分:1)

只需用

替换你的功能
//IP V4

jQuery.validator.addMethod("IP4Checker", function(value, element, param) {
return this.optional(element) || /^(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)$/i.test(value);



//IP V6

jQuery.validator.addMethod("IP6Checker", function(value, element, param) {
return this.optional(element) || /^((([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})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))$/i.test(value);
});