Take the 2-minute tour ×

Question: How would one write a function to check and return whether or not a string (NSString) contains a valid zip code worldwide.

Additional info: I am aware of RegEx in iOS. However I am not so fluent at it. Please keep in mind this should accepts anything valid in any country as true.


US - "10200"

US - "33701-4313"

Canada - "K8N 5W6"

UK - "3252-322"


Edit: Those who voted down or to close the question, please do mention why. Thank you.

share|improve this question
I have not voted down. But I guess that it was because the question shows no research. Re-read the FAQ for StackOverflow for good info on how to ask questions that people will not vote down. :) –  Almo Jun 29 '12 at 14:38

3 Answers 3

up vote 4 down vote accepted


Matches Canadian PostalCode formats with or without spaces (e.g., "T2X 1V4" or "T2X1V4")


Matches all US format ZIP code formats (e.g., "94105-0011" or "94105")

(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]\d[A-Z][- ]*\d[A-Z]\d$)

Matches US or Canadian codes in above formats.

UK codes are more complicated than you think: http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom

share|improve this answer
Canadian postal codes are often written with a -, too, as in T2X-1V4. –  Steven Fisher Jun 28 '12 at 4:48
@StevenFisher - updated –  Ωmega Jun 28 '12 at 11:02
Thank you, it is very complicated indeed. –  Byte Jun 29 '12 at 14:24
I think you want ( |-):? for the Canadian postal code separator. That allows a single space, dash, or nothing. –  Steven Fisher Jun 29 '12 at 16:05

Each country that uses postcodes/zip codes usually has their own format. You are going to be hard-pressed to find a regular expression that matches any worldwide code!

You're better off adding a country picker that determines the regular expression (if any) to be used to validate the zip code.

As an aside, the postcode you have given as a UK example is not correct. A decent UK regex is:

share|improve this answer
why you are expecting lowercase characters in postal code(s)...? –  Ωmega Jun 27 '12 at 16:22
The regular expression is to validate the content of the postcode, not necessarily the format the user entered it (upper/lower case). I would always test the regex against the user's input after transforming it to lowercase. If you want the user to definitely enter the postcode in upper case, you could amend the regex, or use various other methods to enforce it (UITextField delegate methods etc). At the end of the day, the UK postcode is considered valid regardless of case. –  Ian L Jun 27 '12 at 16:42
I see you are from UK, I was just curious, as I have family in UK, I have been doing business with UK companies, but I never ever seen lowercase UK postal code. Thanks for sharing... –  Ωmega Jun 27 '12 at 16:55
I can certainly understand the comment, as UK postcodes are almost always displayed as uppercase, but being in lowercase would not technically make it invalid as to reject it. –  Ian L Jun 27 '12 at 17:51
Thank you lowercase too is valid in my case. +1 for your time. –  Byte Jun 29 '12 at 14:27

I suggest you don't do this. I've seen many websites that try to enforce zipcodes, but I've never seen one get it right. Even the name zipcode is specific to the US.

In other words:

- (BOOL)isValidZipCode: (NSString *)zip {
    return YES;

I was originally going to write [zip length] > 0, but of course even that isn't guaranteed.

share|improve this answer
I actually put in a dummy of something like what you have written here. Sadly, that creates too many false positive. But thank you for trying. –  Byte Jun 29 '12 at 14:25
Good luck. I think what you're trying to do might be achievable, as long as you use custom regexes for each country and spend the effort necessary to test every country's algorithm. Seems like this is something Apple should do. –  Steven Fisher Jun 29 '12 at 16:00

Your Answer


