Verify your zip code in iOS

I am developing an application for iOS 7 in which I want to get user location information. Now I also want the user zip code. And I want to check only that page that the postal code entered by the user is valid or not. I want to approve for USA and UK. How to achieve this?

+2
source share
3 answers

For India, you can use the method below to verify pincode

-(BOOL)isValidPinCode:(NSString*)pincode { NSString *pinRegex = @"^[0-9]{6}$"; NSPredicate *pinTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pinRegex]; BOOL pinValidates = [pinTest evaluateWithObject:pincode]; return pinValidates; } 

For USA you can use

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

For UK use this

 ^([A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW])\ [0-9][ABD-HJLNP-UW-Z]{2}|(GIR\ 0AA)|(SAN\ TA1)|(BFPO\ (C\/O\ )?[0-9]{1,4})|((ASCN|BBND|[BFS]IQQ|PCRN|STHL|TDCU|TKCA)\ 1ZZ))$ 
+3
source

You can use this answer to get the zip code.

Get Current Location Index Code - iPhone SDK

Regarding verification, this will depend on the country in which the postal code is located, but this may be a good starting point.

How to check zip code for USA or Canada in iOS?

0
source

Swift 3.0

For USA, you can use the method below to check the zipcode

 func validZipCode(postalCode:String)->Bool{ let postalcodeRegex = "^[0-9]{5}(-[0-9]{4})?$" let pinPredicate = NSPredicate(format: "SELF MATCHES %@", postalcodeRegex) let bool = pinPredicate.evaluate(with: postalCode) as Bool return bool } 
0
source

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


All Articles