Solved

PHP telephone number input mask

Posted on 2003-12-07
4
3,524 Views
Last Modified: 2012-05-04
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
Comment
Question by:stwd12885
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 3

Accepted Solution

by:
mh2 earned 50 total points
ID: 9895421
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Progress
Starting up a Project

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question