Jump to content

php validate phone number


Hailwood

Recommended Posts

Hi there

 

 

ive been looking into preg_replace,

 

 

how do i validate a phone number in the format of

 

 

^+[0-9]$

is the closest i can get

 

 

it needs to begin with a + can have as many numbers as needed but thats it.

Link to comment
Share on other sites

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'];
}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites


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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.