How to validate phone number using PHP? [duplicate]
Asked Answered
A

3

63

How to validate phone number using php

Antisthenes answered 22/6, 2010 at 6:48 Comment(9)
Please define what makes a phone number valid?Indigence
your missing the other planets..Ritual
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.Alatea
is 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.Greg
@christian: You forgot pause signals.Cahoon
since when did 'phone number start with a tilde ?Kainite
Related: #124059Sienkiewicz
Best solution is to use libphonenumber which is a port of Google's libphonenumber to PHP github.com/giggsey/libphonenumber-for-phpVilayet
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: github.com/brick/phonenumberSesquicarbonate
R
32

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
}
Ritual answered 22/6, 2010 at 6:57 Comment(8)
and people who put brackets around their area code? (202) 555 1212Kainite
Forcing a particular formatting convention is insane. You also need to handle international dialling prefixes.Hiedihiemal
That should be a {3} in the middle of the preg_match, right? Not a {4}. At least for US phone numbers.Fagin
This is not a thorough regex for phone numbers. There are dozens of ways phone numbers can be formatted especially including international numbers.Ogletree
Depending on what they're working on, they might not need international support.Eley
this is not enough, there are many other valid number formatsIdentical
you are forcing a set format this is just bad programming.Villanelle
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.Conduit
M
41

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));
Misdemeanant answered 3/2, 2013 at 17:18 Comment(3)
what if entered number is +1981878978Leandro
@Leandro It only removes the leading 1 if the length is 11.Hydroelectric
Was able to use this with some alterations. thank you for making your answer direct.Doti
R
32

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
}
Ritual answered 22/6, 2010 at 6:57 Comment(8)
and people who put brackets around their area code? (202) 555 1212Kainite
Forcing a particular formatting convention is insane. You also need to handle international dialling prefixes.Hiedihiemal
That should be a {3} in the middle of the preg_match, right? Not a {4}. At least for US phone numbers.Fagin
This is not a thorough regex for phone numbers. There are dozens of ways phone numbers can be formatted especially including international numbers.Ogletree
Depending on what they're working on, they might not need international support.Eley
this is not enough, there are many other valid number formatsIdentical
you are forcing a set format this is just bad programming.Villanelle
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.Conduit
A
17

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

Admiralty answered 22/6, 2010 at 8:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.