How to check validation of IP Address in jquery

前端 未结 9 1760
挽巷
挽巷 2021-01-17 21:39

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?

Thanks

相关标签:
9条回答
  • 2021-01-17 22:02

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

    "127.0.0.1".match(/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/);
    
    0 讨论(0)
  • 2021-01-17 22:03
    /* 
        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

    0 讨论(0)
  • 2021-01-17 22:05

    This function returns true if IP address is in correct format otherwise it returns 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;
    }
    
    0 讨论(0)
  • 2021-01-17 22:08

    I use jQuery Validation Plugin by:

    $.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?

    0 讨论(0)
  • 2021-01-17 22:10

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

    $.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.');
    
    0 讨论(0)
  • 2021-01-17 22:15

    Hi this is the best Solution and Mask for IP Adress

    $.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
                  }
    
              }
            });
    
    0 讨论(0)
提交回复
热议问题