What is the quickest way to validate a postal code as existing on the iPhone?

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?


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.


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.

链接地址: http://www.djcxy.com/p/19226.html

上一篇: iPhone OS:为什么我的managedModelObject不符合Key Value Coding?

下一篇: 验证iPhone上现有邮政编码的最快捷方式是什么?