markthien Posted February 18, 2009 Share Posted February 18, 2009 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 More sharing options...
killah Posted February 18, 2009 Share Posted February 18, 2009 Make 2 different form's like this: Mobile Number: (<input type="text" name="country_code" size="5" />) <input type="text" name="number" size="20" /><br /> Link to comment https://forums.phpfreaks.com/topic/145690-checking-country-dialling-code/#findComment-764909 Share on other sites More sharing options...
markthien Posted February 18, 2009 Author Share Posted February 18, 2009 any better way if I just want only 1 input from user ? Link to comment https://forums.phpfreaks.com/topic/145690-checking-country-dialling-code/#findComment-764932 Share on other sites More sharing options...
niranjnn01 Posted February 18, 2009 Share Posted February 18, 2009 Get all the country codes to a database, use array_slice to cut off the first 3 digits , and check if they match any of the database entry.. http://in2.php.net/manual/en/function.array-slice.php Regards Rakesh Link to comment https://forums.phpfreaks.com/topic/145690-checking-country-dialling-code/#findComment-764939 Share on other sites More sharing options...
angelcool Posted February 18, 2009 Share Posted February 18, 2009 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. Link to comment https://forums.phpfreaks.com/topic/145690-checking-country-dialling-code/#findComment-764962 Share on other sites More sharing options...
The Little Guy Posted February 18, 2009 Share Posted February 18, 2009 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 } } ?> Link to comment https://forums.phpfreaks.com/topic/145690-checking-country-dialling-code/#findComment-764980 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.