Jump to content

BEST WAY TO HANDLE TELE NUMBERS


phppup

Recommended Posts

Is there a "PREFERRED METHOD" to handling telephone numbers gathered by a form?

 

I'm torn between ideas.

 

I need to collect the numbers, and want to be able to view them as (123)555-1212.

 

Should I collect them as a 10 digit ONLY string, and strip them into area code, parenthesis, and hyphenated LATER?  Or gather them as 3 seperated input boxes posted to the DB accordingly?

 

Obviously, entries will have REQUIREMENTS and error messages, so I am focused on the methodology here.  Thanks.

Link to comment
Share on other sites

Why not accept it in any format they want to give it and then just construct it into your own format? For example they can input it as:

(555) 555-5555

(555)5555555

555-555-5555

5555555555

 

It really shouldn't matter how they input it. Strip out all non-numerical characters, make sure it is the appropriate length, and then convert it to your preferred format.

Link to comment
Share on other sites

Didn't know I could do that.  (more info?)

And then, how would I validate it?  Right now I validate by confirming all characters are numeric. 

Also, I have a maimum lengh of 10 characters that would be problematic if people start adding dashes and ().

 

 

Link to comment
Share on other sites

But willl JavaScript be able to validate in a similar manner?

 

Thought I was better validating with JS first to lower the requirements of the server (which has the PHP)?

 

(too much reading... people write stuff in tutorials, then you come here and discover it was all wrong... geesh)

(i wonder if it's more frustrating for the newbies OR the pros!)

Link to comment
Share on other sites

I'm sure there's an easier way to do this but this is my quick attempt:

$num = '(555) 123-4567';

// remove non-numerical characters
$num = preg_replace('/[^\d]/', '', $num);

// count the remaining characters
if (strlen($num) == 10) {
// separate the area code
$areacode = substr($num, 0, 3);
$num = substr($num, 3, strlen($num));

// split the rest into two parts
$num1 = substr($num, 0, 3);
$num2 = substr($num, 3, strlen($num));

// stick everything back together
$num = '(' . $areacode . ') ' . $num1 . '-' . $num2;
}

 

You can do something similar with Javascript just for the user's convenience, but you should still validate server-side because Javascript can't be trusted.

Link to comment
Share on other sites

OK.  Is this code tested?

 

Looks similar to what I was envisioning if I decide to allow input1 as area code, input2 as pre-fix, and input3 as last four digits.

 

Then just put them together and $POST all three as one string (i can do that, can't i????)

 

Thanks for the help.

Link to comment
Share on other sites

OK.  Is this code tested?

 

A little.

 

Then just put them together and $POST all three as one string (i can do that, can't i????)

 

Sure.

$num = $_POST['one'] . $_POST['two'] . $_POST['three'];

 

EDIT: Although I will tell you that having three fields for a phone number is very irritating as an end user. It's not really needed, and makes things like copy/paste difficult.

Link to comment
Share on other sites

You do realize that the 3-digit area-code, 3-digit exchange, 4-digit number is a creation of the North American Numbering Plan Administration (NANPA) which only covers North America, right? If your target audience includes people in other regions, their phone number structure is different.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.