2

In my project i have validate a phone number with country code like if user enter number is 44557788991 and select country US.same like for other country how can i check the phone number is valid or not.

enter image description here

on button click i have to check number is valid or not.

Guillaume Algis
  • 10,705
  • 6
  • 44
  • 72
Chetu
  • 428
  • 1
  • 7
  • 19

2 Answers2

7

I think you want to check the number country wise. for this you have to use following demo

https://github.com/rmaddy/RMPhoneFormat

for UK

    RMPhoneFormat *fmt = [[RMPhoneFormat alloc] initWithDefaultCountry:@"uk"];
    NSString *numberString = // the phone number to format
    NSString *formattedNumber = [fmt format:numberString];

for Australia

RMPhoneFormat *fmt = [RMPhoneFormat instance];
NSString *callingCode = [fmt callingCodeForCountryCode:@"AU"]; // Australia - returns 61
NSString *defaultCallingCode = [fmt defaultCallingCode]; // based on current Region Format (locale)

and so on...

SAMIR RATHOD
  • 3,512
  • 1
  • 20
  • 45
  • this is good but i want to check something like in first textField user enter phone number and then select country code after this i click on button and want to get out put like... Number:1234567890 Code:IN isValidPhoneNumber ? [YES] 164 : +911234567890 INTERNATIONAL : +91 1234 567 890 NATIONAL : 01234 567 890 FC3966 : tel:+91-1234-567-890 extractCountryCode [82] extractCountryCode [82] [3213123123]. i get this out using https://github.com/me2day/libPhoneNumber-iOS. but when i call send time my app is crash @SAMIR RATHOD – Chetu Jun 12 '13 at 11:56
1

You can use NSDataDetector to check if the phone number is valid:

- (BOOL)isValidPhoneNumber:(NSString*)phoneNumber {
    NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber error:nil];
    NSTextCheckingResult *result = [detector firstMatchInString:phoneNumber options:NSMatchingReportCompletion range:NSMakeRange(0, [phoneNumber length])];
    if ([result resultType] == NSTextCheckingTypePhoneNumber) {
        return YES;
    }
    return NO;
}
nice_pink
  • 435
  • 1
  • 3
  • 16