Phone Number Validation

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Phone Number Validation

    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.
    1. 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>
    The validation code

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

    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.

    PHP Code:
    $strPhone 1234567890;  
    $strArea substr($strPhone03); 
    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″.

    PHP Code:
    $strPhone 1234567890;  
    $strPrefix substr($strPhone33); 
    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″.

    PHP Code:
    $strPhone 1234567890;  
    $strNumber substr($strPhone64); 
    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.

    PHP Code:
    $formattedPhoneNum '('.$strArea.') '.$strPrefix.'-'.$strNumber
    BakGat
    Code:
    class Counter {
    public:
      void Count();
      int  ReadDisplay();
    private:
      int  CurrentCount;
    };








    Back up my hard drive? How do I put it in reverse?
    My Community
    BakGat
    sigpic

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

    Comment


      #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 ...
      My site: http://mimobifunclub.tk
      sigpic

      Comment


        #4
        Thats really strange.......... Never came across a error like that on my MXit
        BakGat
        Code:
        class Counter {
        public:
          void Count();
          int  ReadDisplay();
        private:
          int  CurrentCount;
        };








        Back up my hard drive? How do I put it in reverse?
        My Community
        BakGat
        sigpic

        Comment


          #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
          My site: http://mimobifunclub.tk
          sigpic

          Comment


            #6
            does this work for all networks

            does this work for all networks
            http://myfacepals.com
            MYFACEPALS SOCIAL NETWORKsigpic

            Comment


              #7
              Good tutorial. Long, but easy to easy to understand! Thanks!
              mysterio.al - programming is a functional art

              Comment

              Working...
              X