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.
Examples
US – “10200”
US – “33701-4313”
Canada – “K8N 5W6”
UK – “3252-322”
etc.
Edit: Those who voted down or to close the question, please do mention why. Thank you.
^[ABCEGHJKLMNPRSTVXY]\d[A-Z][- ]*\d[A-Z]\d$Matches Canadian PostalCode formats with or without spaces (e.g., “T2X 1V4” or “T2X1V4”)
^\d{5}(-\d{4})?$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