PC Nerd Posted December 30, 2007 Share Posted December 30, 2007 Hi, Im looking for a way to validate a phone number. I'm lost as to how to check if its an integer, and to remove any punctuation it is - and the length of the number. The othe rproblem ive got is that i dont know how many digits ina US mobile phone number. ( Im from Australia.....). If anyone can point me in teh right direction with some functionsetc, id be most greatful. Thanks in advance, PC Nerd Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted December 30, 2007 Share Posted December 30, 2007 There are 10 digits in a US mobile number. There are two ways...you can force the user to conform to a certain format, e.g. ###-###-####, or you can accept all common formats and check for them, e.g.: (###) ###-####, ##########, ###-###-####, etc. With either one, the easiest way would probably be regex to check for format: $pattern = '/^\d{3}-\d{3}-\d{4}$/'; if (preg_match($pattern, trim($phone_number))) { /* Number matches format */ } else { /* Number does not match */ } Repeat ad nausem for other patterns. If using the first method, I would recommend using a JS frontend validation so that the user doesn't have to repeatedly resubmit the form if they make errors. With any method, do (re)validation on the backend. EDIT: There are 10 digits if you don't include the country code. If you do, then most US persons will only put "1 ###...", where as most Europeans will put either "+001..." or "001..." for a US country code. Quote Link to comment Share on other sites More sharing options...
PC Nerd Posted December 30, 2007 Author Share Posted December 30, 2007 ok assuming that someone only enters the numbers withought the spaces or dashes etc ( which is how ill tell them to enter it) - how would i tell how many digits in the value. eg: $_POST['cell_no'] = "1111111111"; how can i tell teh length/dighits in that value? Thanks. Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted December 30, 2007 Share Posted December 30, 2007 Make sure you use "trim" on the user input first to eliminate any leading or trailing spaces. if (is_int($phone_number)) { echo "That phone number is " . strlen($phone_number) . " digits long!!"; } or if (preg_match('/^\d{10}$/', $phone_number)) { echo "That phone number is " . strlen($phone_number) . " digits long!!"; } PHP's weak typing is helpful here. You could also use "is_numeric" to test for the numberness of the value. Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 30, 2007 Share Posted December 30, 2007 Make sure you use "trim" on the user input first to eliminate any leading or trailing spaces. if (is_int($phone_number)) { echo "That phone number is " . strlen($phone_number) . " digits long!!"; } $_POST values are always received as strings, so testing with is_int will always return FALSE. PhREEEk Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted December 30, 2007 Share Posted December 30, 2007 $_POST values are always received as strings, so testing with is_int will always return FALSE. Forgot about that. is_numeric is still a possibility, or you can cast as an int....or just use the regex. Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 30, 2007 Share Posted December 30, 2007 $_POST values are always received as strings, so testing with is_int will always return FALSE. Forgot about that. is_numeric is still a possibility, or you can cast as an int....or just use the regex. Yeah... well, bottomline is, validating a phone number is a layered problem, and it all begins with what your form accepts. I go the slightly more difficult route, but which ends up more end-user friendly, which is, I use 3 input boxes, one that accepts a 3 digit area code, one that accepts a 3 digit CO code, and a last one that accepts a 4 digit extension. I have never had a problem validating a US phone number in this manner, but I don't have a generic version I can copy and paste real quick... I'm just about off to bed, so good luck to the OP! PhREEEk Quote Link to comment Share on other sites More sharing options...
redarrow Posted December 30, 2007 Share Posted December 30, 2007 been working on this try it see what you think.... <?php $a=array("0207-435-7864"); $t=implode(' ',$a); if(strpos($t,'-')){ $m=str_replace('-',' ',$t); if(!eregi("^[0-9]{0,100}$",$m)){ echo"Number Format $m"; }else{ echo"Number format incorrect"; } } ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted December 30, 2007 Share Posted December 30, 2007 standard way <?php if (preg_match('/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/', $phone)) { echo 'Valid'; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.