Reputation: 864
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
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
Reputation: 49720
you can't use NSDataDetector
its available in for only [10_7,4_0]
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