Solved

US Zipcode range validation with jQuery validation plugin

Posted on 2014-12-18
1
175 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
ID: 40507890
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
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)

932 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

10 Experts available now in Live!

Get 1:1 Help Now