jQuery CIDR Input validation
- November 10th, 2009
- Posted in Dojo . Ubuntu . Zend Framework
- By
- Write comment
Dear Lazyweb,
I’m not the top javascript hacker of the pops, but somehow I need an input validation for CIDR addresses.
Preferences:
- JQuery
- JQuery Validation
- an address like this: 192.168.0.0/24
I came up with this snippet:
jQuery.validator.addMethod("cidraddr",function(value,element){console.log(value);return this.optional(element)||/^\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/\d{1,2}\b$/.test(value);},jQuery.validator.format("Please enter a valid CIDR Address!"));
But it’s not enough. If you have a good CIDR regexp, please add it here via comments, or if I didn’t understand the adding of a jquery validator, please add a better one :)
Use the usual communication channels to get in touch with me.
TIA

Hey try this one:
/^
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/(2[5-9]|3[12])
\d{1,3}\.\d{1,3}\.\d{1,3}\.0{1,3}\/(1[789]|2[0-4])
\d{1,3}\.\d{1,3}\.0{1,3}\.0{1,3}\/(9|1[0-6])
\d{1,3}\.0{1,3}\.0{1,3}\.0{1,3}\/[1-8]
0{1,3}\.0{1,3}\.0{1,3}\.0{1,3}\/0
$/
This will match an ip address:
((25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)\.){3}(25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)
or you could write it as
$octet=”(25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)”;
$regex=”${octet}.${octet}.${octet}.${octet}”;