jQuery and ASP.NET

April 27, 2010

Validate IP Address using jQuery




I had recently posted on Add a Custom Validation Method to the jQuery Validation Plugin. A user asked me over twitter asking how to validate IP address using this method.

Here’s the same custom validation method that now validates an IP address. I am using regex to do so (The regular expression was searched on the net)

<html xmlns="http://www.w3.org/1999/xhtml">
<
head>
<
title>Validate IP Address using jQuery</title>
<
script type="text/javascript"
src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js">
</
script>
<
script type="text/javascript" src=
"http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js">
</
script>
<
script type="text/javascript">
$(function() {
$.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
}
}
});

});
</script>
</
head>
<
body>
<
form id="form1">
<
input id="ip" name="ip" type="text" />
<
input id="Submit1" type="submit" value="submit" />
</
form>
</
body>
</
html>

On running the code, enter an IP address. The validation detects an invalid IP and displays the message “Invalid IP address”

Validate IP jQuery

Once you enter a valid IP, you can submit the form

Validate IP jQuery

Live Demo



'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

6 Responses to "Validate IP Address using jQuery"
  1. Beben said...
    April 27, 2010 12:12 PM

    i'll have try it
    and validate my IP...hohohoho

  2. gumuruhsspj said...
    March 16, 2011 10:07 PM

    hohoho...!! nice idea... orite.

    great thanks!

  3. morfey said...
    June 27, 2011 5:02 AM

    Wrong regexp!

    True is:
    $.validator.addMethod('IP4Checker', function(value) {
    var ip = /^(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[0-9]{2}|[0-9])(\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[0-9]{2}|[0-9])){3}$/;
    return value.match(ip);
    });

  4. Ghal Maraz said...
    September 26, 2011 2:21 AM
    This comment has been removed by the author.
  5. Ghal Maraz said...
    September 26, 2011 3:21 AM

    Hi,

    I tried your example and i think a found a little mistake.

    You should user "\\." to escape the dot, because the '\' itselfs needs to be escaped. If you don't it means any character can be a seperator for the ip quartet.

  6. Ray R said...
    January 31, 2012 11:15 AM

    posee errores varios....... grcias de igual forma ya pude adaptarlo

 

Copyright © 2009-2011 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions