Phone number validation using NSDataDetector

1.9k Views Asked by At

How can I validate phone number using NSDataDetector. In my project, the anything higher than 14 text length is also valid but NSDataDetector does not detect it that way.

I am using the code from the following stackoverflow post: NSTextCheckingResult for phone numbers

2

There are 2 best solutions below

4
On BEST ANSWER

you can't use NSDataDetector its available in for only [10_7,4_0]

enter image description here

So its easy and proper way to validate Phone Number Like bellow way instead of NSDataDetector

    NSString *string =@"121453315"; 
    NSString *phoneRegex = @"^((\\+)|(00))[0-9]{6,14}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
    BOOL phoneValidates = [phoneTest evaluateWithObject:string];
0
On

Use NSDataDetector for regular phone numbers, and add a special regex matching only 14-digit+ phone numbers that your app needs to add the missing results. For shorter phone numbers, NSDataDetector will be far more accurate that any regex you may write...