Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check validation of IP Address in jquery

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

like image 750
Rohit Avatar asked Jul 11 '13 07:07

Rohit


5 Answers

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
                    }
                }
            });
like image 122
RONE Avatar answered Nov 16 '22 03:11

RONE


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
            }
        }
    });
like image 20
Abhinav Avatar answered Nov 16 '22 02:11

Abhinav


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}$/);
like image 42
CodingIntrigue Avatar answered Nov 16 '22 02:11

CodingIntrigue


The short version:

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

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

like image 40
oriadam Avatar answered Nov 16 '22 03:11

oriadam


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
              }

          }
        });
like image 1
Spl2nky Avatar answered Nov 16 '22 01:11

Spl2nky