Jump to content

Validating phone number


PC Nerd

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

$_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

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.