I am developing an app for iOS 7 in which I want to retrieve user location details. Now I also want postal code of user. And I want to check on only that page that postal code entered by the user is valid or not. I want to validate for US and UK. How to achieve this?
Asked
Active
Viewed 9,548 times
2
-
You might want to edit the title, what does "pincode" have anything to do with your questions? – pojo Nov 21 '13 at 12:17
-
For what locations do you want validation? You mentioned UK and US is a comment below, but you need to add this to your question. – Peter M Nov 21 '13 at 13:23
-
This will be extremely difficult for the UK. For example blocks of flats can have different postcodes for different floors and GPS will not have the vertical accuracy to distinguish between them. A place to start your research (including links to purchasing postcode info) is the ONS Postal Geography page (make sure you check the related links on the right): http://www.ons.gov.uk/ons/guide-method/geography/beginner-s-guide/postal/index.html – Robotic Cat Nov 21 '13 at 14:00
-
@Plague I'm not sure how `Postal Code` over `Postcode` is really any better. Seems like a very pointless edit and I am surprised it got approved since it was so minor. – Popeye Nov 21 '13 at 15:50
-
@RoboticCat I can't speak for the US but this isn't difficult at all for the UK. Unfortunately I can't share the code but I was part of a small team that developed something very similar if not the exact same thing and we didn't have any trouble doing it. I would probably say it was one of the easiest things to do in the project. – Popeye Nov 21 '13 at 15:59
-
@Popeye:Perhaps I read OP's question wrongly. I thought OP wanted to find a Post Code from a GPS result. If that is what you did, it's amazing that you overcame the GPS height accuracy issue (where you can sometimes be out +/- 23m) for blocks of flats. For normal roads it's not an issue until you near a postcode boundary where GPS might put you in the wrong post code. – Robotic Cat Nov 21 '13 at 16:04
-
1@RoboticCat by the sounds of it you have completely misread the question. They only need to validate the postcode there is nothing to do with GPS location or anything. I think you're really over thinking this. – Popeye Nov 21 '13 at 16:13
-
@RoboticCat yes accordint to popeye...i want to check that is it in correct format or not...and is it correct...means number is not fake it should be in real...i found that in US zip codes are of 10 digit...is it correct???.... http://www.city-data.com/zipDir.html – vivek bhoraniya Nov 22 '13 at 03:25
3 Answers
3
For India, you can use the below method to validate the 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 US, 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))$

manujmv
- 6,450
- 1
- 22
- 35
-
1Doesn't this check only that the format is valid, it doesn't actually check that it is the correct post code for the location. – Abizern Nov 21 '13 at 12:21
-
but manujmv it is only check that code is valid or not....as @Abizern told i have to also check that postcode is correct or not. – vivek bhoraniya Nov 22 '13 at 03:17
-
hello....I have an issue with this...what if user have enter 00000....will this code validate that? – vivek bhoraniya Dec 23 '13 at 10:51
-
1
Swift 3.0
For US, you can use the below method to validate 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
}

Chandu kumar.Alasyam
- 702
- 1
- 8
- 23
0
You can use this answer to get postal code.
Get the Zip Code of the Current Location - iPhone SDK
As for validation, it will depend on the country the postal code is in, but this may be a good starting point.
-
2This is a measure of the problem faced here. The 'ZipCode' is a localisation of a post-code for the United States. Various other schemes exist elsewhere. Unless the app is only ever used with US addresses, breakage will ensue. – marko Nov 21 '13 at 12:34