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

时间:2013-07-11 07:57:12

标签: javascript jquery jquery-mobile

我需要在我的项目中添加IP验证。在jquery或jquery mobile中是否有任何函数。那么它将验证输入字段吗?

由于

9 个答案:

答案 0 :(得分:5)

参考此文件 IP validation

这里他使用了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
                    }
                }
            });

答案 1 :(得分:5)

简短版本:

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

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

答案 2 :(得分:3)

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

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

答案 3 :(得分:1)

嗨,这是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
              }

          }
        });

答案 4 :(得分:1)

这适用于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
            }
        }
    });

答案 5 :(得分:0)

我使用jQuery Validation Plugin:

$.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
        }
    }
});

希望有所帮助。

请参阅:Regular expression to match DNS hostname or IP Address?

答案 6 :(得分:0)

/* 
    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

答案 7 :(得分:0)

除了@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.');

答案 8 :(得分:0)

如果IP地址格式正确,则此函数返回<div class="item"> <div class="two-half"> <div class="wrap">1</div> <div class="wrap">2</div> </div> <div class="two-half"> <div class="wrap">3</div> <div class="wrap">4</div> </div> </div> <div class="item"> <div class="two-half"> <div class="wrap">5</div> <div class="wrap">6</div> </div> <div class="two-half"> <div class="wrap">7</div> <div class="wrap">8</div> </div> </div> ,否则返回true

false