IOS: zip authentication

Question: How to write a function to check and return, regardless of whether the string (NSString) contains a valid zip code worldwide.

Additional Information: I am aware of RegEx in iOS. However, I am not so sure about this. Please keep in mind that this should recognize the valid in any country as true.

Examples

US - "10200"

US - "33701-4313"

Canada - "K8N 5W6"

United Kingdom - "3252-322"

and etc.

Edit: Those who voted or closed the question, please indicate why. Thanks.

+2
source share
3 answers

^[ABCEGHJKLMNPRSTVXY]\d[AZ][- ]*\d[AZ]\d$

Complies with Canadian PostalCode formats with or without spaces (for example, "T2X 1V4" or "T2X1V4")


^\d{5}(-\d{4})?$

Matches all US ZIP code formats (for example, "94105-0011" or "94105")


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

Conforms to US or Canada codes in the above formats.


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

+7
source

I suggest you not do this. I have seen many websites trying to ensure the use of zip codes, but I have never seen that everything is correct. Even the name zipcode refers to the USA.

In other words:

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

I originally intended to write [zip length] > 0 , but, of course, even this is not guaranteed.

+3
source

Each country using postal codes / postal codes usually has its own format. It will be difficult for you to find a regular expression that matches any world!

You better add a country picker that defines the regular expression (if any) that will be used to check the zip code.

As an aside, the zip code you provided as an example of the UK is incorrect. Decent regex in the UK:

 ^(^gir\\s0aa$)|(^[a-pr-uwyz]((\\d{1,2})|([a-hk-y]\\d{1,2})|(\\d[a-hjks-uw])|([a-hk-y]\\d[abehmnprv-y]))\\s\\d[abd-hjlnp-uw-z]{2}$)$ 
+2
source

Source: https://habr.com/ru/post/1439821/


All Articles