Search code examples
iosvalidationnsdatadetector

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


Solution

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