21

Possible Duplicate:
A comprehensive regex for phone number validation

How to validate a phone number (NSString *) in objective-c? Rules:

  • minimum 7 digits
  • maximum 10 digits
  • the first digit must be 2, 3, 5, 6, 8 or 9

Thanks

Community
  • 1
  • 1
ohho
  • 50,879
  • 75
  • 256
  • 383
  • 1
    Objective-C itself is a very minimal language that barely does anything for you — certainly not regular expressions. It's much more relevant to say what libraries you're using (Cocoa, Cocoa Touch, POC, etc). – Chuck Jul 28 '10 at 02:49
  • 1
    My phone number begins with a 0 in the UK or a +44 when I am abroad. – JeremyP Jul 28 '10 at 08:05

5 Answers5

35

You can use a regular expressions library (like RegexKit, etc), or you could use regular expressions through NSPredicate (a bit more obscure, but doesn't require third-party libraries). That would look something like this:

NSString *phoneNumber = ...;
NSString *phoneRegex = @"[235689][0-9]{6}([0-9]{3})?"; 
NSPredicate *test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex]; 
BOOL matches = [test evaluateWithObject:phoneNumber];

If you're on iPhone, then iOS 4 introduced NSRegularExpression, which would also work. The NSPredicate approach works on both Mac and iPhone (any version).

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • Just had to one-up me, didn't you :) – Joshua Weinberg Jul 28 '10 at 02:51
  • It doesn't even work across the whole US, so I'd assume the OP has a precisely narrow use case he's dealing with. – Ben Zotto Jul 28 '10 at 03:52
  • Yes, it's a specific narrow use case I am looking for. – ohho Jul 28 '10 at 04:56
  • 1
    What is the meaning of the '?' (last char of phoneRegex)? – ohho Jul 28 '10 at 08:43
  • 1
    It means "none or one" of the last group. Conceptually, you'd probably think of the optional digits as coming at the beginning for a 10- vs 7-digit phone number, but since you have a requirement on the first digit, it's easiest to call the last three digits the optional ones. – Seamus Campbell Aug 02 '10 at 00:04
23

Please don't use your own regex for the phone-number. Don't.

The format of phone numbers changes across the country, and your app might be used outside of your own country.

Instead, use what Apple provides, as Josh says. See here.

Yuji
  • 34,103
  • 3
  • 70
  • 88
11

For those searching for phone extraction, you can extract the phone numbers from a text, for example:

NSString *userBody = @"This is a text with 30612312232 my phone";
if (userBody != nil) {
    NSError *error = NULL;
    NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber error:&error];
    NSArray *matches = [detector matchesInString:userBody options:0 range:NSMakeRange(0, [userBody length])];
    if (matches != nil) {
        for (NSTextCheckingResult *match in matches) {
            if ([match resultType] == NSTextCheckingTypePhoneNumber) {
                DbgLog(@"Found phone number %@", [match phoneNumber]);
            }
        }
    }
}

`

Rafael Sanches
  • 1,823
  • 21
  • 28
7

The NSDataDetector class, available in iOS 4.0 and later, is a specialized subclass of NSRegularExpression that has explicit support for detecting phone numbers.

Shaggy Frog
  • 27,575
  • 16
  • 91
  • 128
5

In iOS 4.0+ there are built in classes to do this, NSRegularExpression

In everything else you can either use a 3rd party RegEx library, or use an NSPredicate if your needs are narrow enough

Joshua Weinberg
  • 28,598
  • 2
  • 97
  • 90