Solved

PHP telephone number input mask

Posted on 2003-12-07
4
3,509 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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?
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

729 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