• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1119
  • Last Modified:

Telephone Number Validation

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!

Ben

0
souldigital
Asked:
souldigital
  • 2
2 Solutions
 
cr4ck3rj4ckCommented:
Hey there,

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

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

Open in new window

0
 
souldigitalAuthor Commented:
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

447525867821

Thanks!

Ben
0
 
James LooneySr. Programmer/AnalystCommented:
to strip whitespace, you could do a number of different things:



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

Open in new window

0
 
souldigitalAuthor Commented:
That works great!

Thanks guys!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now