Solved

US Zipcode range validation with jQuery validation plugin

Posted on 2014-12-18
1
198 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

Question has a verified solution.

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

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
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…

719 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