jQuery IP地址验证失败 [英] jQuery IP address validation failed

查看:73
本文介绍了jQuery IP地址验证失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Below is the code snippet where I have written the code to have the IP address masked and validate it using jQuery. But, I am getting error message for valid IP addresses (like 222.222.222.222). Can someone please help?

            //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;
                });
            });

推荐答案

您的正则表达式缺少|字符,即

You regex is missing its | characters ie.

\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]?)

还请注意,您可以将其缩短为

Also note you could shorten this to

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

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

If you dont need to check it is a valid ip address between 0-255

任何\本身都需要转义,所以它将\

also any \ needs to be escaped itself so it will be \

参考

更新

OP显示小提琴并且没有在行上方读过,并且蒙版的两侧都留有空格.因此正则表达式永远无效,因为掩码会添加空格.

OP showed fiddle and hadnt read above line also mask had spaces either side of the . so the regex was never valid as the mask added the spaces.

固定小提琴

这篇关于jQuery IP地址验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆