Phone Number Validation

Started on the old forum in April 2010 · view the original in the archive · old links redirect here

6 replies · 1,852 views · started 16 years ago · 1 min read

5 participants
bOrN2pwn Developer · 230 posts #1

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?

  1. If the user submitted dashes, parenthesis, spaces or any character that its not a number, remove them.

  2. Check that you are left with exactly 10 numbers.

Let’s code:

The Form

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

The validation 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!';  
        }  
    }  
}  

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.

WATCHOUT! 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.

$strPhone = 1234567890;  
$strArea = substr($strPhone, 0, 3); 

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″.

$strPhone = 1234567890;  
$strPrefix = substr($strPhone, 3, 3);  

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″.

$strPhone = 1234567890;  
$strNumber = substr($strPhone, 6, 4); 

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.

$formattedPhoneNum = '('.$strArea.') '.$strPrefix.'-'.$strNumber; 

BakGat

class Counter {
public:
  void Count();
  int  ReadDisplay();
private:
  int  CurrentCount;
};

External image (may be gone) External image (may be gone) External image (may be gone)

Back up my hard drive? How do I put it in reverse? http://www.hackthissite.org Profile…

neostar Developer · 109 posts #2

Good 1. But how to validate that the no is owned by user who posted?

mobidev Senior · 312 posts #3

Can't you get the sim number from the device used ? But I guess that will validate the device with number and sim card number. I had a problem on a chat app a while ago (mxit)-born u know this app I guess. My problem was I had registered years ago with a certain phone number but don't really use that sim anymore. I use it only in an old fone just for service provider messages as I'm a cellular salesman. My problem occured when I bought my blackberry and installed mxit onto it and tried cellular number as login with pascode that I would normally use in any other phone. My blackberry kept telling me authentication failed due to device sim and application numbers not corresponding. I struggled about a month on mxit forums and decided to register new account with current sim and am able to use the app now ...

mobidev Senior · 312 posts #5

That's what the guys at mxit told me aswell and after a month of trieng I opted to register with the sim in my phone and it works here is another thing if I put the ol sim in my blackberry I can't login to my new account as it validates it as not the correct number... Weird but true lol. So now I just use my normal sim

Mysterio Lead · 630 posts #7

Good tutorial. Long, but easy to easy to understand! Thanks!

mysterio.al - programming is a functional art

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close