2

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?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
vivek bhoraniya
  • 1,526
  • 2
  • 17
  • 36
  • 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 Answers3

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
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
    }
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.

how to validate Zipcode for US or Canada in iOS?

Community
  • 1
  • 1
pojo
  • 5,892
  • 9
  • 35
  • 47
  • 2
    This 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