9

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

Rohit
  • 685
  • 3
  • 13
  • 28

9 Answers9

10

refer this document IP validation

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

            $.validator.addMethod('IP4Checker', function(value) {
                return value.match(/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/);
            }, 'Invalid IP address');

            $('#form1').validate({
                rules: {
                    ip: {
                        required: true,
                        IP4Checker: true
                    }
                }
            });
Jai Kumaresh
  • 715
  • 1
  • 7
  • 33
RONE
  • 5,415
  • 9
  • 42
  • 71
  • 1
    Hi your mask does not work please try this one /^(\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]))$/; – Spl2nky Aug 15 '14 at 16:22
  • Not working correctly. Mask is not correct. Check: https://www.oreilly.com/library/view/regular-expressions-cookbook/9780596802837/ch07s16.html – VSB Feb 06 '20 at 17:14
5

The short version:

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

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

Community
  • 1
  • 1
oriadam
  • 7,747
  • 2
  • 50
  • 48
4

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}$/);
CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
  • 2
    doesn't really validate an ip: "999.0.0.1".match(/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/); – Reason Jul 11 '13 at 08:01
  • @Rohit Reason is right, that's not a valid IP. But you get the idea, you might need a more involved regex if you want to test for valid IPs – CodingIntrigue Jul 11 '13 at 08:06
2

This should work for IP address

$.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
            }
        }
    });
Abhinav
  • 323
  • 1
  • 3
  • 11
1

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
              }

          }
        });
Spl2nky
  • 614
  • 5
  • 10
0

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?

Community
  • 1
  • 1
Till
  • 1,097
  • 13
  • 13
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

Jayakumar
  • 91
  • 1
  • 14
0

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.');
hsuk
  • 6,770
  • 13
  • 50
  • 80
0

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;
}
VSB
  • 9,825
  • 16
  • 72
  • 145