简体   繁体   中英

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM