Link to home
Start Free TrialLog in
Avatar of lynnwalker
lynnwalker

asked on

US Zipcode range validation with jQuery validation plugin

The jQuery validation plugin additional methods provides an example for zipcode range validation. This same example appears within numerous posts on the internet as an example of doing zipcode range validation, but I have found no discussion as to how to expand the range beyond the narrow scope used in this example.

jQuery.validator.addMethod("ziprange", function(value, element) {
    return this.optional(element) || /^90[2-5]\d\{2\}-\d{4}$/.test(value);
}, "Your ZIP-code must be in the range 902xx-xxxx to 905-xx-xxxx");

How would I rewrite this function, using regex or otherwise, to validate the range of valid US zipcodes (00544 - 99500) while still allowing for 9-digit zips? I mention this final condition because I have tried using the more basic "range" validation [544, 99950] but it fails to accommodate 9-digit zips and invalidates any input of 6 or more digits.
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial