phppup Posted March 16, 2012 Share Posted March 16, 2012 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. Quote Link to comment Share on other sites More sharing options...
scootstah Posted March 16, 2012 Share Posted March 16, 2012 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. Quote Link to comment Share on other sites More sharing options...
phppup Posted March 16, 2012 Author Share Posted March 16, 2012 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 (). Quote Link to comment Share on other sites More sharing options...
scootstah Posted March 16, 2012 Share Posted March 16, 2012 Also, I have a maimum lengh of 10 characters that would be problematic if people start adding dashes and (). That's why you check the length after stripping that stuff out. Quote Link to comment Share on other sites More sharing options...
phppup Posted March 16, 2012 Author Share Posted March 16, 2012 Ahhhhhhhhh. Can you give me a code example or web-link. I'm learning (at least I think i am... LOL). Quote Link to comment Share on other sites More sharing options...
phppup Posted March 16, 2012 Author Share Posted March 16, 2012 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!) Quote Link to comment Share on other sites More sharing options...
scootstah Posted March 16, 2012 Share Posted March 16, 2012 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. Quote Link to comment Share on other sites More sharing options...
phppup Posted March 16, 2012 Author Share Posted March 16, 2012 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. Quote Link to comment Share on other sites More sharing options...
scootstah Posted March 16, 2012 Share Posted March 16, 2012 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. Quote Link to comment Share on other sites More sharing options...
phppup Posted March 16, 2012 Author Share Posted March 16, 2012 I was thinking more like: $areacode + $prefix + $lastfour = $_POST[telenumber'] Are my mechanics running backwards? This way the DB would have everything as desired in one field. Quote Link to comment Share on other sites More sharing options...
DavidAM Posted March 16, 2012 Share Posted March 16, 2012 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. Quote Link to comment Share on other sites More sharing options...
phppup Posted March 17, 2012 Author Share Posted March 17, 2012 Didn't realize that, but YES, primarily 2 local counties; and CERTAINLY all USA. But thank you, as it's always beneficial to have knowledge like this to draw on. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.