Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3600
  • Last Modified:

PHP telephone number input mask

I need to use some kind of mask to force user enter the correct telephone number format like xxx-xxx-xxxx. I am using PHP and MySQL.

Thank you in advance for you help.
0
stwd12885
Asked:
stwd12885
1 Solution
 
mh2Commented:
I am not sure how you can do this in the browser, perhaps a javascript event that fires when the user presses a key in the input box, then check if it is the correct character according to your input rules.

On the php side, this is easier. Once the user has submitted the form, you can use a regex to compare the input from the telephone field to validate its format.

Something like:

function isValidTelephoneNum( $tel_number, $desired_format = "/^\d{3}-\d{3}-\d{4}$/" )
{
    return preg_match( $desired_format, $tel_number );
}


if ( !isValidTelephoneNum( $_POST['telephone'] ) )
{
    // do failure processing
}
else
{
   // do success processing
}

No matter what you put in front-end html for browser, it is a good idea to validate it in the PHP code to ensure what you put in you database is correct, you can never trust what you receive from the browser no matter what you sent in your HTML form/javascript.

HTH
Mark
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now