There are different formats in which forms present you with a way to input your phone number. Some have a 3-digit field between parenthesis for the area code, a second 3-digit field and finally a third 4-digit field.
Some coders like to record the dashes and the parenthesis, like this: (123) 456-7890 or 123-456-7890 or simply 456-7890
Let’s say you recorded the phone number using the format that uses parenthesis. You have hundreds, maybe even thousands of records. Now imagine your boss now wants to display dashes and no parenthesis; or a client changes his mind and wants to display spaces instead of dashes or parenthesis. You can see you will have a complicated depuration process ahead.
Personally, I prefer to have only one field that requests for a 10 digit phone number (no dashes, no parenthesis), validate it and record only 10 numbers in the database.
Since only numbers were inputted in the database, when the record needs to be displayed, I can easily format it any way I want.
What is it that you have to check for when validating US phone numbers?
Let’s code:
The Form
The validation code
Let’s say that you already successfully record phone numbers in your database. How would you go about displaying them?
Format the phone number
There are different preferences: dashes, parenthesis or spaces. Let’s say you want to format the phone number using a combination of the three, like this: “(123) 456-7890″.
An incredible deed? No, just learn how to use the PHP: substr - Manual function.
The PHP: substr - Manual function returns part of a string. It takes 3 paramenters: the input string, the start position and the length.
Let’s use this function to disect the 10-digit phone number in 3: the area code, the following 3 digits and the last 4 digits.
The above code will allow us to get the first 3 digits of the phone number. This is what we are telling http://php.net/manual/en/function.substr.php: get me 3 characters starting from character position 0. So $strArea will contain “123″.
This is what we are telling http://php.net/manual/en/function.substr.php: get me 3 characters starting from character position 3. So $strPrefix will contain “456″.
This is what we are telling substr: get me 4 characters starting from character position 6. So $strNumber will contain “7890″.
These three examples should give you an idea of how PHP: substr - Manual works with positive values.
So we have disected the 10-digit string into $strArea, $strPrefix and $strNumber. Now we just have to add the parenthesis, spaces and dashes were applicable.
Some coders like to record the dashes and the parenthesis, like this: (123) 456-7890 or 123-456-7890 or simply 456-7890
Let’s say you recorded the phone number using the format that uses parenthesis. You have hundreds, maybe even thousands of records. Now imagine your boss now wants to display dashes and no parenthesis; or a client changes his mind and wants to display spaces instead of dashes or parenthesis. You can see you will have a complicated depuration process ahead.
Personally, I prefer to have only one field that requests for a 10 digit phone number (no dashes, no parenthesis), validate it and record only 10 numbers in the database.
Since only numbers were inputted in the database, when the record needs to be displayed, I can easily format it any way I want.
What is it that you have to check for when validating US phone numbers?
- If the user submitted dashes, parenthesis, spaces or any character that its not a number, remove them.
- Check that you are left with exactly 10 numbers.
Let’s code:
The Form
PHP Code:
<FORM name="directory" action="<?php echo $_SERVER['PHP_SELF'].' ?>" method="POST">
<INPUT type="text" name="name">
<INPUT type="text" name="phone" value="<?php if(isset($_POST['phone'])){echo $_POST['phone'];} ?>">
<INPUT type="hidden" name="process" value="1">
<INPUT type="submit" value="Register my phone">
</FORM>
PHP Code:
if($_POST['process']==1) {
if(emptyempty($_POST['phone'])) {
echo '
<P style="color:#C00">Please enter your phone number.
';
exit();
}
else {
// Set the number of numeric values on the phone number.
$characters = 10;
$strPhone = $_POST['phone'];
// Remove not numeric characters
$strPhone = ereg_replace("[^0-9]", "", $strPhone);
// Check that $strPhone contains the specified number of numeric characters
if(strlen($strPhone)!=$characters) {
echo 'Please enter a phone number with exactly '.$characters.' digits long.';
exit();
}
else {
echo 'Thank you for submitting a valid 10-digit phone number!';
}
}
}
Format the phone number
There are different preferences: dashes, parenthesis or spaces. Let’s say you want to format the phone number using a combination of the three, like this: “(123) 456-7890″.
An incredible deed? No, just learn how to use the PHP: substr - Manual function.
The PHP: substr - Manual function returns part of a string. It takes 3 paramenters: the input string, the start position and the length.
WATCHOUT!
substr starts counting from 0, not from 1.
substr starts counting from 0, not from 1.
Let’s use this function to disect the 10-digit phone number in 3: the area code, the following 3 digits and the last 4 digits.
PHP Code:
$strPhone = 1234567890;
$strArea = substr($strPhone, 0, 3);
PHP Code:
$strPhone = 1234567890;
$strPrefix = substr($strPhone, 3, 3);
PHP Code:
$strPhone = 1234567890;
$strNumber = substr($strPhone, 6, 4);
These three examples should give you an idea of how PHP: substr - Manual works with positive values.
So we have disected the 10-digit string into $strArea, $strPrefix and $strNumber. Now we just have to add the parenthesis, spaces and dashes were applicable.
PHP Code:
$formattedPhoneNum = '('.$strArea.') '.$strPrefix.'-'.$strNumber;
Comment