Validate phone number ios

52,368

Solution 1

NSString *phoneRegex = @"^((\\+)|(00))[0-9]{6,14}$";

This way is bit better. Your code will work as well if you escape the "\".

Solution 2

Copy & Paste method to validate phone numbers:

- (BOOL)validatePhone:(NSString *)phoneNumber
{
    NSString *phoneRegex = @"^((\\+)|(00))[0-9]{6,14}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];

    return [phoneTest evaluateWithObject:phoneNumber];
}

or Open source Library for validating Phone Numbers

https://github.com/iziz/libPhoneNumber-iOS

Solution 3

NSString *phoneRegex = @"^((\\+)|(00))[0-9]{6,14}|[0-9]{6,14}$";

This is tested RegularExpression This will accept with country code OR without country code

Solution 4

The only good way to validate phone numbers is to use Google's amazing LibPhoneNumber. There's an iOS port, or you can run the JavaScript implementation in a hidden UIWebView.

(I've done the latter years ago when their was no iOS port yet. Works like a charm and is very fast even on old iPhones.)

Solution 5

well it depends on how strict you want to be it doesn't seem like this regex is especially strict. this regex says:

start at beginning of line match one + (or maybe 1 or 0) which seems ambiguous (but may not be depending on implementation) because the capture parentheses:() breaks up the relationship of the + and the ? possibly misplaced : match any digit 0-9 1 or 0 times 6-14 times then one digit 0-9 then end of line. also you note that any backslash will have to be doubled... @"\b" for a word boundary. you may want to try something like...

@"\\b[\\d]{3}\\-[\\d]{3}\\-[\\d]{4}\\b"
would I think match your example, but it wouldn't match
(555) 555 - 5555 or
555.555.5555 or
+44 1865  55555
Share:
52,368

Related videos on Youtube

Thomas Clayson
Author by

Thomas Clayson

Updated on July 22, 2022

Comments

  • Thomas Clayson
    Thomas Clayson almost 2 years

    I need to validate an international phone number.

    I know its difficult to validate an international phone number, so I'm going to keep it simple:

    + or 00 then 6-14 numbers

    My current code using a regex isn't working for some reason which I can't work out. It just says that it cannot open the regex and crashes.

    Here is my current code:

    NSString *phoneRegex = @"^[\+(00)][0-9]{6,14}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
    
    BOOL phoneValidates = [phoneTest evaluateWithObject:phoneNumber];
    

    Where am I going wrong?

    Thanks!

  • Thomas Clayson
    Thomas Clayson over 11 years
    Not sure I understand. Have you copied an answer from another
  • Thomas Clayson
    Thomas Clayson over 11 years
    The \ was escaping the +... do I still need to escape the +?
  • whiteagle
    whiteagle over 11 years
    you need to escape the slash first to "enter" within the string. the \\ will result in \ for regexp.
  • GuillaumeS
    GuillaumeS almost 9 years
    Would it be possible to use a phone validation library such as this one : link ? Very complete and offers a lot of services.
  • Pavan
    Pavan almost 9 years
    But this only works for countries following the ITU recommendation. Japan, the united status and a few other countries use a different prefix. Am I correct in thinking that your code would not cater for these countries? If so, perhaps modify your code to accept a variety of prefixes as opposed to just 00? en.wikipedia.org/wiki/List_of_international_call_prefixes
  • Pradip Vanparia
    Pradip Vanparia almost 8 years
    @vikramjain if phone number have alphabets then how can we parse it? Like Apple Inc phone number is 1800MYAPPLE. Any regex for it?
  • Atef
    Atef almost 8 years
    Rocket ,, Bingo Thanks :)
  • turingtested
    turingtested over 7 years
    Why are the parentheses needed here, wouldn't it be equally good with @"^(\\+|00)[0-9]{6,14}$";?
  • alexbt
    alexbt over 7 years
    Welcome to Stack Overflow! An answer was already provided and accepted (4 years ago) for this question! Does your answer bring something new ? If so, you should explain why.
  • meaning-matters
    meaning-matters over 7 years
    Does not support international phone numbers.
  • benc
    benc almost 6 years
    I think that's a good suggestion, but the ask was specifically about how to get a piece of code to work. I've worked on specific bits of validation where we had narrow requirements, and a library was overkill.
  • meaning-matters
    meaning-matters almost 6 years
    @benc A 'piece of code' will always fail because of the complexity of telephone numbering. There's no other way than to use a library. Having a quick look at the main resource file LibPhoneNumber should convince you immediately.
  • benc
    benc almost 6 years
    I've tested more than a couple phone number handlers, I don't need a lot of convincing. But sometimes, the person asking isn't going to be able to use that, even if its the best solution.
  • meaning-matters
    meaning-matters almost 6 years
    @benc It's not a matter of good and better, but of incorrect and correct. Of course someone asking is free to use incorrect phone number formatting and annoy users.
  • DrMickeyLauer
    DrMickeyLauer over 2 years
    Beware. This crashes in iOS 15.