简体   繁体   English

iOS中的邮政编码验证

[英]Postal Code Validation in iOS

I am developing an app for iOS 7 in which I want to retrieve user location details. 我正在为iOS 7开发一个应用程序,我想在其中检索用户位置详细信息。 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? 如何实现呢?

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))$

Swift 3.0 斯威夫特3.0

For US, you can use the below method to validate the ZipCode 对于美国,您可以使用以下方法来验证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
    }

You can use this answer to get postal code. 您可以使用此答案获取邮政编码。

Get the Zip Code of the Current Location - iPhone SDK 获取当前位置的邮政编码-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? 如何在iOS中验证美国或加拿大的Zipcode?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM