views:

361

answers:

6

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?

A: 

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.

Lex
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
A: 

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.

Nat Ryall
NANP http://en.wikipedia.org/wiki/North_American_Numbering_Plan specifies valid numbers for the U.S. and Canada.
Robert Claypool
A: 

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

Cesar
Excuse me, I interpreted the question wrong.
Cesar
+2  A: 

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.

ceejayoz
What do you propose as a way to determine if a user inputted phone # is well formed?
JacobT
Depends on the level of validity you need. If it's mission critical, I'd have a system like http://www.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
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
@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
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
A: 

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.

Peter Bailey
Stripping non-numeric characters would remove the issue with various formats.
ceejayoz
A: 

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.

Bart Kiers
A more common issue with stripping all characters would be someone who enters their extension in a format like `###-###-#### x####`.
ceejayoz