3
<input type="text" id="zipCode" />

how can i validate that this is a valid zip code in javascript?

4
  • 1
    Is this anything more than a regular expression to match 5 digits? Commented Apr 7, 2011 at 19:37
  • @Babak - If you want to be thorough there are additional constraints. Commented Apr 7, 2011 at 19:38
  • 2
    Does "valid zip code" mean "real zip code"? There are also rules that depend on what sort of address it is ... Commented Apr 7, 2011 at 19:41
  • 1
    stackoverflow.com/questions/578406/… Commented Apr 7, 2011 at 19:42

6 Answers 6

7

You can match the input against this regex.

/^\d{5}(-\d{4})?(?!-)$/

US zip codes are 5 digits followed by an optional 4 digits.

Sign up to request clarification or add additional context in comments.

2 Comments

This should be /^\d{5}(-\d{4})?(?!-)$/ instead. Otherwise if you have any numeric string greater than 5 digits it will match that too.
var ZipVal = (/^\d{5}(-\d{4})?(?!-)$/.test($(this).val()));
4

A while ago I had a client that needed to find out if an address was within 30 miles of a type of shipping facility. They needed to filter thousands of zip-codes that a client might have. So we could write a web-service to transport thousands of zip-codes back-and-forth... instead, I came up with this regex:

/(0(2(1([0-2]\d|3[0-7])|84[01])|3217|403[2-4]|5751|61(0\d|1[0-2])|7039)|1(00(0\d|4[0-8])|52([0-3]|4[0-4])|990[1-5])|2(00([0-1]\d|20)|12(0[1-9]|[1-2]\d|3[0-7])|4517|5813|7565|9029)|3(03(0[1-9]|[1-7]\d|8[01])|250[1-9]|28(0[1-9]|[1-2]\d|3[0-7])|31(2[4-9]|[3-8]\d|90)|58(0[1-9]|1[0-6])|72(0[1-9]|1\d|2[0-2])|953[0-5])|4(170[12]|41(0[1-9]|[1-7]\d)|620[1-9]|9036|973[4-5])|5(03(0[1-9]|1\d|2[0-3])|280[1-9]|32(0[1-9]|1\d|2[0-8])|580[1-8]|740[1-2]|8282|9044)|6(06(0[1-9]|[1-3]\d|4[01])|270[1-9]|31(0[1-9]|[1-3]\d|4[01])|72(0[1-9]|1\d|2[01])|890[12])|7(011[2-9]|22(0[1-9]|1[0-7])|41(0[1-9]|10)|870[1-5])|8(02(0[1-9]|[1-3]\d)|2941|3254|423[1-3]|50(0[1-9]|[1-4]\d|5[0-5])|750[0-6]|95(0[1-9]|1[0-3]))|9(00(0[1-9]|[1-9]\d)|0209|021[0-3]|420[3-9]|68(0[1-9]|[12]\d|30)|72(0[1-9]|1\d|2[0-5])|800[4-9]|95(0[1-9]|1\d|2[0-4])))/

and tested against it. If you really want to, you could theoretically do the same thing with every valid zip-code :)

1 Comment

That's impressive. It's so overkill, I'm impressed.
1

ZipCodes differ all over the world.

The most common way to validate input is via regular expressions. Read up on them here.

Regexes in Javascript specifically here.

1 Comment

As Adam states in his answer, ZIP codes are not the same as postal codes. They are a subset, and do not differ
0

The best that you can achieve without a web service is to determine whether the zip code might be valid assuming you also know the country. For example, the US zip code 00001 is valid from the perspective of containing five numbers but is not an actual zip code. The only way to truly know if a given value represents a real zip code is to look it up using something like the USPS web services.

USPS Address Information API

Comments

-1

ZIP codes are not the same as postal codes. You should be aware that postal codes are different in different countries and some don't have any.

To validate US zip codes you can do a simple validation that the field contains a 5 digit zip code or a 9 digit zip code with an optional dash after the first 5 digits. Of course that provides no guarentee that that zip code actually exists.

To validate that the zip code entered actually exists you can either keep a list of valid zip codes or valid zip ranges on the page to check from which will take up space and you will need to keep updated or you can use a web service.

Geonames has a webservice which can check zip codes.

The jQuery Validation plug-in could be used to do a simple number validation.

Comments

-1

I don't think REGEX is best because it still won't tell you if it's valid or not. Our company, GreatData.com, provides a free ZIP Code database that's updated quarterly that you can load into your database and skip the need for a web service. It even has some basic latitude and longitude if you need to calculate the distance (as cwolves mentioned above).

If you do want to use REGEX, make sure that you factor in that some ZIP Codes have one or two leading zeroes that may get stripped out by some programs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.