PHP: How to validate a phone number if well formed?

Using PHP, how can I verify if a phone # is well formed?

It seems easiest to simply strip all non-numeric data, leaving only the numbers. Then to check if 10 digits exist.

Is this the best and easiest way?

13.10.2009 17:28:15
Some people don't live in the +1 zone: my phone numbers all begin +44. International numbers don't necessarily have exactly 10 digits after the country code. Of course, there are plenty of good reasons for not wanting to make international calls ;-)
Steve Jessop 13.10.2009 17:37:16
Does this assume that you are only looking for US/Canadian phone numbers? Once you get to international prefixes (+49 for Germany) and/or specific country rules (e.g. in Germany not all phone numbers have the same length) things get really tricky.
Thorsten 13.10.2009 17:38:45
6 ОТВЕТОВ

You can use a regex for it:

$pattern_phone = "|^[0-9\+][0-9\s+\-]*$|i";
if(!preg_match($pattern_phone,$phone)){
    //Somethings wrong
}

Haven't tested the regex, so it may not be 100% correct.

0
13.10.2009 17:30:23
It depends on the OP's requirement. But I doubt it's 100% correct. Your regex will match strings like + or 0---------- or 9 9, to name just a few bogus numbers.
Bart Kiers 13.10.2009 17:38:44

Checking for 10 digits after stripping will check the syntax but won't check the validity. For that you'd need to determine what valid numbers are available in the region/country and probably write a regex to match the patterns.

0
13.10.2009 17:31:06
NANP en.wikipedia.org/wiki/North_American_Numbering_Plan specifies valid numbers for the U.S. and Canada.
Robert Claypool 13.10.2009 19:03:50

What format you need? You can use regular expressions to this.

-2
13.10.2009 17:31:19

The best? No. Issues I see with this approach:

  • Some area codes - like 000-###-#### - are not valid. See http://en.wikipedia.org/wiki/List_of_NANP_area_codes
  • Some exchanges - like ###-555-#### - are not valid. See http://en.wikipedia.org/wiki/555_%28telephone_number%29
  • Some people will enter a 1 before their number, i.e. 1-###-###-####.
  • Some people are only reachable at an extension, like ###-###-#### x####.
  • Some companies tack on extra digits, like 1-800-GO-FLOWERS. The additional digits are simply ignored by the phone system, but a user might expect to be able to enter the whole thing.
  • International phone numbers are not necessarily 10 digits, even if you discount the country codes.

Good enough? Quite possibly, but that's up to you and your app.

2
13.10.2009 17:36:38
What do you propose as a way to determine if a user inputted phone # is well formed?
JacobT 13.10.2009 17:34:35
Depends on the level of validity you need. If it's mission critical, I'd have a system like twilio.com attempt to initiate a real phone call to the number. If I'm not using the data for anything important, your approach might suffice. It all depends on the app.
ceejayoz 13.10.2009 17:36:01
I propose that you don't check the phone number. If they mess up, let the user deal with the consequences. Contrary to popular belief, people don't mess up in forms that often, and when they do, usually catch their mistake before submitting it.
Thomas Owens 13.10.2009 17:36:44
@Thomas Owens And if you do validate, perhaps just a warning - "your phone number is in a format we don't recognise, please double-check it".
ceejayoz 13.10.2009 17:39:20
That would be acceptable to me. But unless I have a formal definition of valid input/output in front of me, I try to give the users as much leeway with how they format their inputs as I possibly can. It usually leads to a leaner and more easily maintained codebase if I don't try to validate and clean everything up.
Thomas Owens 13.10.2009 17:40:57

The problem with validating/filtering data like this usually comes down the the answer to this question: "How strict do I want to be?" which then devolves into a series of "feature" questions

  • Are you going to accept international numbers?
  • Are you going to accept extensions?
  • Are you going to allow various formats i.e., (111) 222-3333 vs 111.222.3333

Depending on your business rules, the answers to these questions can vary. But to be the most flexible, I recommend 3 fields to take a phone number

  1. Country Code (optional)
  2. Phone Number
  3. Extension (optional)

All 3 fields can be programmatically limited/filters for numeric values only. You can then combine them before storing into some parse-able format, or store each value individually.

0
13.10.2009 17:34:54
Stripping non-numeric characters would remove the issue with various formats.
ceejayoz 13.10.2009 17:37:01

Answering if something is "the best" thing to do, is nearly impossible (unless you're the one answering your own question).

The way you propose it, stripping all non-digits and then check if there are 10 digits, might result in unwanted behaviour for a string like:

George Washington (February 22, 1732 – December 14, '99) was the commander of the Continental Army in the American Revolutionary War and served as the first President of the United States of America.

since stripping all non-digits will result in the string 2217321499 which is 10 fdigits long, but I highly doubt that the entire string should be considered as a valid phone number.

0
13.10.2009 17:36:28
A more common issue with stripping all characters would be someone who enters their extension in a format like ###-###-#### x####.
ceejayoz 13.10.2009 17:38:12