Solved

PHP telephone number input mask

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
This is about my first experience with programming Arduino.
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now