PHP Form Validation - Phone Number format - 0123456789

Posted on 2006-06-27
Last Modified: 2009-09-24
Hi Experts,

I need to validate the value of a text box.  The text box is for the customers phone number in the format 0123456789 (ie 10 numbers only)

The data is sent to a page (validate.php) via a forms POST method.

I want a function that will ONLY accept 10 numbers one after another, no letters or characters.

Thanks in advance.
Question by:etechnicsit
LVL 49

Accepted Solution

Roonaan earned 100 total points
ID: 16998863
Here you go:

if(!preg_match('/^\d{10}$/', $_POST['phonenumber'])) {
} else {


Expert Comment

ID: 16998968

if (!is_numeric(substr($_POST['phonenumber']), 0, 10))) {
} else {


if (!is_numeric($_POST['phonenumber']) || 10 !== strlen($_POST['phonenumber'])) {
} else {

Author Comment

ID: 16998990
Works like a charm!

Sorry ixti first in best dressed

Expert Comment

ID: 25411405
hello i have problem in ie6.
is there any one to solve my problem..

is there any code for ie6 compatibility?

this is my site

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

810 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