1

下面是我编写的代码片段,用于屏蔽 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;
                });
            });
4

2 回答 2

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 地址

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

参考

更新

OP 显示了 fiddle 并且没有阅读上面的行,并且掩码的两侧都有空格。所以正则表达式永远不会有效,因为掩码添加了空格。

固定小提琴

于 2012-07-02T14:26:53.923 回答
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);
});
于 2013-02-04T12:37:07.447 回答