jQuery 如何在jquery中检查IP地址的验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17587994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:21:25  来源:igfitidea点击:

How to check validation of IP Address in jquery

javascriptjqueryjquery-mobile

提问by Rohit

I need to add IP Validation in my project .Is there any function in jquery or in jquery mobile.So that it will validate the in put field?

我需要在我的项目中添加 IP 验证。在 jquery 或 jquery mobile.So 中是否有任何功能,以便它验证输入字段?

Thanks

谢谢

采纳答案by RONE

refer this document IP validation

请参阅本文档 IP 验证

here he has used jqueryvalidator.js and explained with example.

在这里他使用了 jqueryvalidator.js 并用示例进行了解释。

 $.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);
            }, 'Invalid IP address');

            $('#form1').validate({
                rules: {
                    ip: {
                        required: true,
                        IP4Checker: true
                    }
                }
            });

回答by oriadam

The short version:

简短版本:

^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|$)){4}$

Explained here https://stackoverflow.com/a/26445549/3356679

在这里解释 https://stackoverflow.com/a/26445549/3356679

回答by CodingIntrigue

You can use regular expressions to test if an IP is valid:

您可以使用正则表达式来测试 IP 是否有效:

"127.0.0.1".match(/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/);

回答by Abhinav

This should work for IP address

这应该适用于 IP 地址

$.validator.addMethod('IP4Checker', function(value) {

    var ip="^([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])$";
        return value.match(ip);
    }, 'Invalid IP address');

    $('#remoteForm').validate({
        rules: {
            ipAddr: {
                required: true,
                IP4Checker: true
            }
        }
    });

回答by Spl2nky

Hi this is the best Solution and Mask for IP Adress

嗨,这是 IP 地址的最佳解决方案和掩码

$.validator.addMethod('IP4Checker', function(value) {
        var ip = /^(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\.(\d|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))$/; 
        return value.match(ip);
        }, 'Invalid IP address');

    var $validator = $("#addCardForm").validate({
          rules: {
              txtIP: {
              required: true,
              IP4Checker: true
              }

          }
        });

回答by Jayakumar

/* 
    validIpAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
    validHostnameRegex = "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$";
*/
$.validator.addMethod('ipChecking', 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]?)$";
    validIpAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
    ipCheckFlag = true;
    ipLists = value.split(',');
    for(ip=0; ip<ipLists.length; ip++){
        if(!ipLists[ip].trim().match(validIpAddressRegex)){
            ipCheckFlag = false;
        }
    }
    return ipCheckFlag;     
});

link

关联

回答by hsuk

Addition to answer by @RAVI MONE for IP address with subnet mask:

除了@RAVI MONE 对带有子网掩码的 IP 地址的回答:

$.validator.addMethod('IP4Checker', function(value) {
    var ip="^$|([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])((/([01]?\d\d?|2[0-4]\d|25[0-5]))?)$";
    return value.match(ip);
}, 'Invalid IP address.');

回答by VSB

This function returns trueif IP address is in correct format otherwise it returns false:

true如果 IP 地址格式正确,则此函数返回,否则返回false

function isIpAddressValid(ipAddress) {
    if (ipAddress == null || ipAddress == "")
        return false;
    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]?)$';
    if (ipAddress.match(ip) != null)
        return true;
}

回答by Sean

I use jQuery Validation Plugin by:

我通过以下方式使用 jQuery 验证插件:

$.validator.addMethod('IP4Checker', function(value) {
        var ip = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
            return value.match(ip);
}, 'Invalid IP address');

$('#form').validate({
    rules:{
        ip:{
            required: true,
            IP4Checker: true
        }
    }
});

Hope helpful.

希望有帮助。

Refer to: Regular expression to match DNS hostname or IP Address?

请参阅:正则表达式匹配 DNS 主机名或 IP 地址?