2
votes

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.

2
Did you mean you want to check the number exists or not ? - Maulik
i want to check number by country code like user select US then number according to number format. @Maulik - chetu
The question itself is silly. - El Tomato

2 Answers

7
votes

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

1
votes

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