开发者

validate zip code in javascript

<input type="text" id="zipCode" />
开发者_如何学C

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


You can match the input against this regex.

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

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


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 :)


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.


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜