5

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?

Baby Groot
  • 4,637
  • 39
  • 52
  • 71
  • it is working for me nicely: `\\([0-9]{3}\\)\\s[0-9]{3}-[0-9]{4}`. – holex May 02 '13 at 13:35
  • or... if you want to make the whitespace optional only: `\\([0-9]{3}\\)\\s?[0-9]{3}-[0-9]{4}` – holex May 02 '13 at 13:46
  • or... if you want to allow the multi-whitespaces: `\([0-9]{3}\)(\s*)?[0-9]{3}-[0-9]{4}`... or I should have created and answer for them, shouldn't I? – holex May 02 '13 at 13:51

4 Answers4

3

Replace your pNRegex with this,

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

Hope it helps you.

MadhuP
  • 2,019
  • 17
  • 22
3

Maybe a better solution would be to strip all non-numeric characters of the phone number for validation so that you're just left with a string of numbers.

Regular expressions can break easily unless you are very strict with their input and since phone numbers vary so greatly around the world, this way will allow you to support any phone number without having to create a very complicated regular expression.

You can just use a simple method like this:

- (NSString *)sanitizePhoneNumberString:(NSString *)phoneNumber {
    NSCharacterSet *illegalCharacters = [NSCharacterSet characterSetWithCharactersInString:@" +()-"];
    return [[phoneNumber componentsSeparatedByCharactersInSet:illegalCharacters] componentsJoinedByString:@""];
}

So instead of +1 (555) 123-4567

You'd just compare 15551234567. If you're storing this in a database, you probably want to control the format anyways for showing on the UI and integer comparison is faster than string comparison for large data sets so you can just store the [sanitizedPhoneNumber intValue]; in the database as an NSNumber

Another method using a simple Regex to allow only the numbers 0-9 (source):

- (NSString *)sanitizePhoneNumberString:(NSString *)phoneNumber {
    return [phoneNumber stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [phoneNumber length])];
}
Community
  • 1
  • 1
iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
1

Use this expression, it worked for me

NSString *pNRegex = @"\\([0-9]{3}\\) [0-9]{3}\\-[0-9]{4}";
Prasad Devadiga
  • 2,573
  • 20
  • 43
1

check this one it'l works fine for your formatt,

if you want to give special chrectars like space,(,) then append that charectar with //

    NSString *phone=@"(123)123-1234";
    NSString *pNRegex = @"\\([0-9]{3}\\)[0-9]{3}\\-[0-9]{4}";
    NSPredicate *PNTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pNRegex];
    BOOL check=[PNTest evaluateWithObject:phone ];
    NSLog(@"%i",check);

O/P:-

(123)123-1234  - check=1

(123123-1234   - check=0

(123) 123-1234 - check=0

if you want to give space after end bracket use this one,

NSString *phone=@"(123) 123-1234";
    NSString *pNRegex = @"\\([0-9]{3}\\)\\ [0-9]{3}\\-[0-9]{4}";
    NSPredicate *PNTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pNRegex];
    BOOL check=[PNTest evaluateWithObject:phone ];
    NSLog(@"%i",check);

O/P:-

(123) 123-1234  - check=1

(123123-1234   - check=0

(123)123-1234 - check=0
Balu
  • 8,470
  • 2
  • 24
  • 41