views:

110

answers:

3

I have an iPhone application which accepts valid zip codes as input. The input can be quickly validated for format; I am looking for methods to validate whether it exists or not.

What is the most efficient way to test whether a given Canadian or US postal code exists, without storing a database of valid codes on the phone?

+1  A: 

Have a look here for a regex that validates the postal code.

Edit: Here's the proper regexp that validates three types of Postal Codes, 5 digit US ZIP code, 5 digit US ZIP code + 4, and 6 digit alphanumeric Canadian Postal Code. The first link that I included originally is incorrect as it does not validate the canadian postal code...ooops. Thanks to David Kanerek for the point out.

Hope this helps, Best regards, Tom.

tommieb75
He specifically included Canadian postal codes which use letters.
David Kanarek
+1 for the correction and most useful answer here.
David Kanarek
+1  A: 

You can use a regex, but that will only tell you if the code is in the valid format, but doesn't tell you if the zip code actually exists. For example, "99999" is a valid US zip code format (5 digits), but is not a valid zip code.

I'd find some online service that allows you submit a zip code and returns you the cities that correspond to it. Some online weather services (Yahoo, Weather.com) have a developer API that is pretty easy to integrate.

Dave DeLong
+1 for calling out to a web service. I've done this with similar requirements, except I used the National Weather Service's database.
Erik Forbes
I can validate the format already. I'm looking for methods to check whether the (valid) postal code exists
blueberryfields
A: 

Using UPs.com to validate your data is illegal in case you did not know it.

Christine Kaplan