Telephone Number Validation

Posted on 2008-11-13
Last Modified: 2012-05-05
I want to validate a number posted into a variable from a html form.

      $dest1; = $_REQUEST["mobile"];

So a html form posts the mobile number into the variable dest1

I then need it to check/modify dest1 so it looks like - 447525000000

Rather than the standard - 07525000000

Then post the checked/modified number into variable dest2

I have never used PHP before, I am sure this is very simple but I need it ASAP :-)

Thanks In advance!


Question by:souldigital
    LVL 6

    Accepted Solution

    Hey there,

    The code below drops the leading 0, if present, and prepends 44. Otherwise it outputs identical to $dest1.

    Hope that helps,
    $dest1 = $_REQUEST["mobile"];
    $dest2 = (substr($dest1,0,1) == '0') ? '44'.substr($dest1,1) : $dest1;
    echo $dest2;

    Open in new window


    Author Comment

    That's great thanks! Is there any way of telling it to drop +44 if entered and replace it with just 44 as well?

    Also is it possible to remove gaps, for example if someone entered -

    07 525 000000

    it would turn it into



    LVL 6

    Assisted Solution

    to strip whitespace, you could do a number of different things:

    $number = str_replace(" ", "", $_REQUEST["mobile"]);
    $number = preg_replace('/\s+/', '', $_REQUEST["mobile"]);

    Open in new window


    Author Comment

    That works great!

    Thanks guys!

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Suggested Solutions

    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    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…

    755 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

    22 Experts available now in Live!

    Get 1:1 Help Now