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:{ …
[Read more]