Anand
Anand

Reputation: 864

Phone number validation using NSDataDetector

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

Upvotes: 0

Views: 1917

Answers (2)

Thomas Deniau
Thomas Deniau

Reputation: 2573

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

Upvotes: 0

Nitin Gohel
Nitin Gohel

Reputation: 49720

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

Upvotes: 1

Related Questions