jzaefferer/jquery-validation — Issue #265: Yet another method, netipv4
Author: ldardini
Created: 2011-11-28 17:01:59+00:00
State: open

Hello,
I'd like to contribute a new method, extending the current ipv4 method. The current limitation for ipv4 is it is restricted to entering an IP address. The following method, named netipv4 will validate either an IP (v4) or a network in compact or extensive format, like 192.168.100.100 or 192.168.0.0/16 or 192.168.0.0/255.255.255.0

jQuery.validator.addMethod("netipv4", function(value, element, param) {
return this.optional(element) || /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)((\/3[0-2]|\/[0-2]?[0-9]|(\/25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))?)$/i.test(value);
}, "Please enter a valid IP v4 address or network.");

It was the first time I use jQuery, so maybe this is a trivial addon for you... but just in case...

Leandro