How to validate phone number using php
-
1Please define what makes a phone number *valid*? – Gordon Jun 22 '10 at 06:58
-
4is this an Australian phone number, a UK number, an international number, etc, etc? Validating phone numbers from around the world can open up more worms than email validation - and I don't think I've ever seen that done 100% infallible. – HorusKol Jun 22 '10 at 07:15
-
Related: http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation – Maxime Pacary Feb 26 '13 at 16:43
-
2your missing the other planets.. – Ben Rowe Jun 22 '10 at 06:58
-
In my country they don't. Plus there might be an international prefix. Or an extension. Depending on the country, digit groups get distanced with spaces, dots or dashes. Ergo: Validation is culture dependend and not quite as simple as you think. – Christian Studer Jun 22 '10 at 06:58
-
@christian: You forgot pause signals. – Alix Axel Jun 22 '10 at 08:39
-
since when did 'phone number start with a tilde ? – Mawg says reinstate Monica Aug 27 '11 at 02:03
-
2Best solution is to use libphonenumber which is a port of Google's libphonenumber to PHP https://github.com/giggsey/libphonenumber-for-php – jbrahy Aug 22 '19 at 19:35
-
It's not good this topic was closed in favor of topic about phone number regexps, because it suggest there is no better way to achieve this. And there are many. For PHP right now the most straightforward way is to use this library: https://github.com/brick/phonenumber – Konrad Gałęzowski Oct 08 '20 at 13:27
3 Answers
Here's how I find valid 10-digit US phone numbers. At this point I'm assuming the user wants my content so the numbers themselves are trusted. I'm using in an app that ultimately sends an SMS message so I just want the raw numbers no matter what. Formatting can always be added later
//eliminate every char except 0-9
$justNums = preg_replace("/[^0-9]/", '', $string);
//eliminate leading 1 if its there
if (strlen($justNums) == 11) $justNums = preg_replace("/^1/", '',$justNums);
//if we have 10 digits left, it's probably valid.
if (strlen($justNums) == 10) $isPhoneNum = true;
Edit: I ended up having to port this to Java, if anyone's interested. It runs on every keystroke so I tried to keep it fairly light:
boolean isPhoneNum = false;
if (str.length() >= 10 && str.length() <= 14 ) {
//14: (###) ###-####
//eliminate every char except 0-9
str = str.replaceAll("[^0-9]", "");
//remove leading 1 if it's there
if (str.length() == 11) str = str.replaceAll("^1", "");
isPhoneNum = str.length() == 10;
}
Log.d("ISPHONENUM", String.valueOf(isPhoneNum));

- 2,976
- 2
- 17
- 14
-
3
-
-
Was able to use this with some alterations. thank you for making your answer direct. – levi Feb 09 '17 at 20:57
Since phone numbers must conform to a pattern, you can use regular expressions to match the entered phone number against the pattern you define in regexp.
php has both ereg and preg_match() functions. I'd suggest using preg_match() as there's more documentation for this style of regex.
An example
$phone = '000-0000-0000';
if(preg_match("/^[0-9]{3}-[0-9]{4}-[0-9]{4}$/", $phone)) {
// $phone is valid
}

- 28,406
- 6
- 55
- 75
-
27and people who put brackets around their area code? (202) 555 1212 – Mawg says reinstate Monica Aug 27 '11 at 02:04
-
25Forcing a particular formatting convention is insane. You also need to handle international dialling prefixes. – Quentin Feb 03 '12 at 15:53
-
4That should be a {3} in the middle of the preg_match, right? Not a {4}. At least for US phone numbers. – Dan Goodspeed Jun 21 '14 at 19:48
-
7This is not a thorough regex for phone numbers. There are dozens of ways phone numbers can be formatted especially including international numbers. – Jake Wilson Sep 26 '14 at 15:06
-
Depending on what they're working on, they might not need international support. – M H Feb 26 '19 at 21:40
-
-
-
Here is a regular expression for US phone numbers with optional 1 at the front and optional parens, also accepts - or . or ' ' between numbers or no spacer: ^1?[-. ]?\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$ I think the number of digits in the regular expression of the accepted answer is off XXX-XXXX-XXXX second {N} should be 3. – Jason Hitchings Oct 05 '20 at 05:07
I depends heavily on which number formats you aim to support, and how strict you want to enforce number grouping, use of whitespace and other separators etc....
Take a look at this similar question to get some ideas.
Then there is E.164 which is a numbering standard recommendation from ITU-T

- 1
- 1

- 4,747
- 2
- 32
- 34