Hi everyone!

I'm working on a page that adds a new client. One of the form's values is a telephone number. The user enters a 10 digit number with no symbols (ex: 2288675309). Before it goes into the DB(MySQL), I want to format it so that it has hyphens (ex: 228-867-5309). After the user clicks submit, I have some javascript error checking to make sure the values are valid. If it is valid, I'm currently trying to format the number after the checks, using document.getElementById("telephone").value. I know I'm changing that value, but it doesn't change what is stored in the database. The query is made in PHP, using $_REQUEST. Any direction would be helpful!


Sidenote, not related: I may have adept programming skills in other things, but not web programming. I have about 6 months or less real experience, but hey, got to learn sometime right? :)

Recommended Answers

All 8 Replies

Member Avatar for diafol

Are these tel numbers always in the same format? Could they possibly start with a 0? Client-side validation is all well and good, but you need server-side validation as well. A preg_match() using regex could be used to check its validity.

Format will always be xxx-xxx-xxxx. However it seemed better to put the hyphens in and just make the user enter the numbers. I am using regex to check that client side, but before I attempt to send it, I wanted to add the hyphens.

All well and good but American phone number format if different from most of rest of the world. for example Australia xx-xxxx-xxxx and UK xxxx-xxx-xxx. Also in europe it is normal to add the ISD code so a french number might be +33 xxx-xxx-xxxx which means that you would have to look for the'+'

The phone number does not matter!! Only accessing the form data and modifying it successfully before running the query. This will only be used by a small, local, american group.

its easier to store unmanipulated unformatted data,
if the small local american site becomes a large global site there is less recoding of rigid code
the database is smaller by 2bytes, not a big thing for 10 records a big thing for many fields of millions of records
a datetime stamp takes 4bytes(?), a text date time takes
monday Feb 16 2011, 11:05:45pm (much more than 4) etc

<?php echo subst($_request['telephone'],0,3).'-'.subst($_request['telephone'],3,3).'-'.subst($_request['telephone'],6,4); ?>

I really do agree with you guys. Its just that's what they want. I'll try the what you posted tomorrow almostbob!

if you dont tell them the code, only show them the output.....
will they notice

You are right, they wouldn't notice! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.