Validate phone number ios
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
Related videos on Youtube
Thomas Clayson
Updated on July 22, 2022Comments
-
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 numbersMy 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!
-
Mick MacCallum over 11 years
-
Thomas Clayson over 11 yearsThanks - this is why I'm keeping it simple. The API call will validate it completely, I just want to restrict the number of calls if possible.
-
Guillaume Algis almost 11 yearsRelated, better answer IMO: stackoverflow.com/questions/17063278
-
meaning-matters over 7 yearsBest way is this.
-
-
Thomas Clayson over 11 yearsNot sure I understand. Have you copied an answer from another
-
Thomas Clayson over 11 yearsThe \ was escaping the
+
... do I still need to escape the+
? -
whiteagle over 11 yearsyou need to escape the slash first to "enter" within the string. the \\ will result in \ for regexp.
-
GuillaumeS almost 9 yearsWould it be possible to use a phone validation library such as this one : link ? Very complete and offers a lot of services.
-
Pavan almost 9 yearsBut 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 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 almost 8 yearsRocket ,, Bingo Thanks :)
-
turingtested over 7 yearsWhy are the parentheses needed here, wouldn't it be equally good with
@"^(\\+|00)[0-9]{6,14}$";
? -
alexbt over 7 yearsWelcome 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 over 7 yearsDoes not support international phone numbers.
-
benc almost 6 yearsI 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 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 almost 6 yearsI'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 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 over 2 yearsBeware. This crashes in iOS 15.