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?
Replace your pNRegex with this,
NSString *pNRegex = @"^(\\([0-9]{3}\\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}$";
Hope it helps you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With