Jump to content

Checking country dialling code


markthien

Recommended Posts

Hi guys,

 

    I have just recently started to use php so kindda new kid on the block. My site will signup user using their mobile phone. Hence, when user enter their mobile phone number like 44123456789 or 353123456789123, where the first 2 digits (44) and first 3 digit (353) is the country dialling code. So how am I able to verify using php script if this 1st 2 or 3 digits is a valid country dialling code by given a list of like 250 valid country code?

 

Appreciate any help here please.

 

Thanks and reagrds,

Mark

Link to comment
https://forums.phpfreaks.com/topic/145690-checking-country-dialling-code/
Share on other sites

Here:

 

<?php
//This function only takes one parameter($tel_number): the user supplied tel number.
//If it is a valid telephone number returns the country dialing code.
function get_country_code($tel_number)
  {
    $number_of_digits=strlen($tel_number);

if($number_of_digits==11)
	  {  
		$country_code=substr($tel_number,0,-9);
		 if ($country_code=='44')
			return $country_code;//country code is 44
	  }
   elseif($number_of_digits==15)
		{
			 $country_code=substr($tel_number,0,-12);
			 if ($country_code=='353')//country code is 353
				return $country_code;
		}
	 else
	   echo 'User did not supply a valild telephone number';
   
  
  }
  
//echo get_country_code('44123456789');

//we will call the function with the supply tel number by user.
echo get_country_code('353123456789123');
?>

 

I hope it'll be helpful.

The HTML:

<form action="send.php" method="post">
     Phone: <input type="text" value="xxx-xxx-xxxx" name="phone" /><input type="submit" value="Send" />
</form>

 

The PHP:

<?php
$phone = explode('-',$_POST['phone']);
list($p1, $p2, $p3) = explode('-',$_POST['phone']);
if(count($phone) == 3){
     if(strlen($p1) == 3 && ctype_digit($p1)){
        // First one is good
    }
     if(strlen($p2) == 3 && ctype_digit($p2)){
        // Second one is good
    }
     if(strlen($p3) == 4 && ctype_digit($p3)){
        // Third one is good
    }
}
?>

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.