0

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?

blueberryfields
  • 45,910
  • 28
  • 89
  • 168

2 Answers2

1

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.

t0mm13b
  • 34,087
  • 8
  • 78
  • 110
1

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
  • 242,470
  • 58
  • 448
  • 498
  • 1
    +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 Jan 27 '10 at 21:26
  • I can validate the format already. I'm looking for methods to check whether the (valid) postal code exists – blueberryfields Jan 27 '10 at 21:29
  • also note that a (valid US) zip code can have a dash (-) and 4 additional digits, after the first 5 digits – John Smith Sep 28 '17 at 14:55