Simple jQuery: how to validate IPv4 addresses and netmasks

Unfortunately jQuery doesn’t come with default form validation to check for ip-addresses or subnet masking. So without a long winded explanation here’s the code. Just include this as a separate JS file like the rest of your page’s JS.

// 'ipv4': IPv4 Address Validator
$.validator.addMethod('ipv4', function(value) {
    var ipv4 = /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/;    
    return value.match(ipv4);
}, 'Invalid IPv4 address');

// 'netmask': IPv4 Netmask Validator
$.validator.addMethod('netmask', function(value) {
    var mask = /^[1-2]{1}[2,4,5,9]{1}[0,2,4,5,8]{1}\.
[0-2]{1}[0,2,4,5,9]{1}[0,2,4,5,8]{1}\.
[0-2]{1}[0,2,4,5,9]{1}[0,2,4,5,8]{1}\.
[0-9]{1,3}$/;    
    return value.match(mask);
}, 'Invalid IPv4 netmask');

You can use it like this.

$("#myform_here").validate({
    rules:{
	ipaddress:{
	    required:true,
	    ipv4:true
	},
	netmask:{
	    required:true,
	    netmask:true
	}
.
.
.
Tagged , ,

8 thoughts on “Simple jQuery: how to validate IPv4 addresses and netmasks

  1. 3tokoj says:

    999.999.999.999 is not a valid IPv4 address.
    You should state somewhere that this validator is not exact.
    BTW, IP address regex is often used as an example of the trade-off between simplicity and exactness. You’ll find more precise regexes on the web.

  2. admin says:

    Disclaimer: this regex validates input for number based quad notation. It does not care if you put in 999.999.999.999. Buyer beware, free code is free code and I never claimed it was perfect for everyone’s use case but it works for my use case where I do sever-side validation by checking to see if the submitted IP is alive via arping; but I didn’t mention that because the post isn’t about server side input validation.

    And yes, you’re exactly right, IP regex is a trade off between simplicity and exactness so you’ve hit the nail on the head.

  3. Barry says:

    Why dont you allow 0′s in the first quad?

    50.19.19.19 is a valid IP address.

  4. admin says:

    Good catch, I’ve updated the regex.

  5. Barry says:

    Ah, now what about 8.8.8.8? :)

    If you allow the other quads to be 0 prefixed, why not the first? I’ve seen systems, that insist they be written with the 0′s, 098.231.004.034

  6. admin says:

    Interesting, I’ve never come across a system that wants full quad notation starting the first quad with a 0 but I’ll take your word that they exist; the regex can be easily changed to allow for that :) … as well as address the 8.8.8.8 discussion (updated post). So now we’re back to a basic [0-9].[0-9].[0-9].[0-9] syntax.

  7. mido says:

    how to make validate with no require
    … pass filed when it empty

  8. admin says:

    Set required:false

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>