Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSPredicate for phone number

My phone number format is (987) 786-5645.

- (BOOL)validatePhoneNumberWithString:(NSString*)phone
{
    NSString *pNRegex = @"[(0-9)]{3}+\\ +\\[0-9-]{3}+\\[0-9]{4}";
    NSPredicate *PNTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pNRegex];
    return [PNTest evaluateWithObject:phone];
}

I am using above code.

What should be the exact regex rule for this? I am able to figure out digits, and brackets also.

What should I use for detecting that whitespace after closing bracket?

like image 495
Baby Groot Avatar asked May 02 '13 12:05

Baby Groot


1 Answers

Replace your pNRegex with this,

NSString *pNRegex = @"^(\\([0-9]{3}\\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}$";

Hope it helps you.

like image 118
MadhuP Avatar answered Oct 07 '22 12:10

MadhuP