4

After some searching, I haven't been able to find a definite answer.

If I am given a zip code like 11111, it is technically a valid zip code but it doesn't exist. Is there a way to check this? The same goes for a postal code like a1a a1a (valid but not real).

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Siva
  • 1,256
  • 3
  • 13
  • 29
  • 1
    Check out the jQuery Validation library: http://stackoverflow.com/questions/19011993/how-to-validate-us-zip-code-using-jquery-validation-plugin – jlewkovich Nov 24 '14 at 21:29
  • 2
    You should probably include which country you're talking about or if this is regarding any countries specifics – Bowdzone Nov 24 '14 at 21:29
  • 2
    The USPS has an API (usable for a fee) for validating actual ZIP codes. As ZIP codes can be added as needed, they would be the only reliable source for validity. – Ken White Nov 24 '14 at 21:31
  • 1
    "The same goes for a postal code like a1a a1a (valid but not real)." What country is this valid in? It looks almost like a UK postcode, but the second part always starts with a digit, so it wouldn't be valid here. "A1A 1AA" is technically a valid form, I think. – IMSoP Nov 24 '14 at 21:49
  • That was a Canadian one. Looks like the only solution is to compare it against a dataset. I was just foolish in thinking there would be a way to check using a few rules XD. – Siva Nov 24 '14 at 22:23

2 Answers2

6

For United States zipcodes:

You can purchase / download databases of zip codes, which you can use in your local app.

Google for

us zip code database

Here is one that you can use for free for non-commercial purposes (for commercial purposes you can purchase a cheap license):

http://www.unitedstateszipcodes.org/zip-code-database/

There are some providers which allow you to perform remote HTTP calls, search around

Cody Caughlan
  • 32,456
  • 5
  • 63
  • 68
1

For any who stumble onto this and are using Python, this exists: https://github.com/Brobin/usps-api

It doesn't cover all the API functionality, but it makes the standard stuff much easier than constructing XML by hand.

Robert Muil
  • 2,948
  • 1
  • 24
  • 30