Solved

PHP telephone number input mask

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

12 Experts available now in Live!

Get 1:1 Help Now