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
  • 687
  • 3
  • 12
  • 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
  • 572
  • 1
  • 5
  • 28
RONE
  • 5,213
  • 8
  • 38
  • 69
  • 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
  • @JaiKumaresh If it is not working for you and you know then right answer then you may comment here and you can give your own answer because it was working answer then and accepted by OP. So, try to give answer rather then edit working and existing answer. – Xitiz Aug 17 '21 at 10:36
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
  • 6,735
  • 2
  • 43
  • 44
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
  • 71,301
  • 29
  • 165
  • 172
  • 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
  • 313
  • 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
Sean
  • 1,017
  • 11
  • 12
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
  • 101
  • 1
  • 13
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,571
  • 12
  • 48
  • 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
  • 8,918
  • 12
  • 64
  • 128