Solved

US Zipcode range validation with jQuery validation plugin

Posted on 2014-12-18
1
173 Views
Last Modified: 2015-01-07
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.
0
Comment
Question by:lynnwalker
1 Comment
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
jQuery.validator.addMethod("ziprange", function(value, element) {
    var v = value.replace(/^90[2-5]|-/g, "") * 1; // remove third first digits and dash and create a numeric 
    var testZip = v > = 544 && v < 99500 && /^90[2-5]\d\{2\}-\d{4}$/.test(value);
    return this.optional(element) || testZip;
}, "Your ZIP-code must be in the range 902xx-xxxx to 905-xx-xxxx");

Open in new window

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now