Jump to content

php validate phone number


Hailwood

Recommended Posts

If they all, without fail, begin with a +, and contain a string of digits of no certain length, it would probably be easiest to just validate the form field as numeric, and concatenate the + to the string in the code.

 

I.E., have the user enter the number (1234567890), then

 

if(is_numeric($_POST['tel_no'])) {
     $tel_no = "+";
     $tel_no .= $_POST['tel_no'];
}

If they all, without fail, begin with a +, and contain a string of digits of no certain length, it would probably be easiest to just validate the form field as numeric, and concatenate the + to the string in the code.

 

I.E., have the user enter the number (1234567890), then

 

if(is_numeric($_POST['tel_no'])) {
     $tel_no = "+";
     $tel_no .= $_POST['tel_no'];
}

 

So 1.234e56 is a valid phone number in your eyes?


if (substr($_POST['tel_no'], 0, 1) == '+')
{
   $num = substr($_POST['tel_no'], 1);
}
else
{
   $num = $_POST['tel_no'];
}

if (!ctype_digit($num))
{
   echo $num . ' is not a valid telephone number.';
}
else
{
   $num = '+' . $num;
//...
}

 

//edit missed a ; lol

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.