How would I get this php code to validate the phone number before sending?

Discussion in 'Programming' started by dubs89, Nov 29, 2010.

  1. #1
    How would I get this php code to validate the phone number before sending?

     
    dubs89, Nov 29, 2010 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    Write here what type of phone numbers you consider as valid phone numbers.
     
    s_ruben, Nov 29, 2010 IP
  3. dubs89

    dubs89 Peon

    Messages:
    231
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    anything XXX-XXX-XXXX or XXX XXX XXXX (hyphens dont really matter if they are there or not)
     
    dubs89, Nov 30, 2010 IP
  4. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #4
    You need to list each unique property of a valid phone number, then test your phone number variable for those properties using php.

    With what you have so far, these are the properties I see:

    1. 12 characters in length.
    2. 4th and 8th characters are spaces or hyphens.

    Both of those are trivially easy to test for in php. However, I think you need to think about your properties more--see if you can create an invalid phone number that has all your properties.

    Is 123-abc-@#$% a valid phone number? If not, then you need to define and check for invalid characters.
    Is 7128675309 a valid phone number even though its only 10 characters long? If so, refine properties #1 & #2.
    Is 712.867.5309 a valid phone number even though it uses periods? If so, refine property #2.

    Keep trying to break your properties with invalid phone numbers until your properties are satisfactory and then code a test for each property in php.
     
    plog, Nov 30, 2010 IP
  5. Cozmic

    Cozmic Member

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    48
    #5
    class phoneNumber {
        public $nums;
        
        function __construct($num) {
            $this->set($num);
        }
        
        function set($num) {
            $num = preg_split('//', $num);
            foreach ($num as $key => $value) if (!is_numeric($value)) unset($num[$key]);
            
            if (count($num) < 10 OR count($num) > 11) {
                trigger_error("WOOPS! The phone number is not valid. The length ".count($num)." isn't enough.");
                exit;
            }
            
            $num = array_merge(array(), $num);
            $this->nums = $num;
        }
        
        function get() {
            $nums = $this->nums;
            if (count($nums) == 11) {
                $num = $nums[1].$nums[2].$nums[3]."-".$nums[4].$nums[5].$nums[6]."-".$nums[7].$nums[8].$nums[9].$nums[10];
            } else if (count($nums) == 10) {
                $num = $nums[0].$nums[1].$nums[2]."-".$nums[3].$nums[4].$nums[5]."-".$nums[6].$nums[7].$nums[8].$nums[9];
            }
            return $num;
        }
    }
    PHP:
    I just wrot ethis class for 'ya. Here's the usage:

    
    $somePhoneNumber = "1 234 567 8901"
    $pn = new phoneNumber($somePhoneNumber);
    echo $pn->get();
    
    PHP:
    All it does is store all the numbers inside the input into an array. If the array isn't the right size for a phone number (10 or 11), then it uses trigger_error() to show an error message. The get() function displays the formatted phone number which is just the array formatted as a phone number.
     
    Last edited: Nov 30, 2010
    Cozmic, Nov 30, 2010 IP
  6. dubs89

    dubs89 Peon

    Messages:
    231
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Nice! Thanks!

    Now umm how do i go about adding that to my code? Sorry kinda a php newbie here...

    Thanks though that's awesome!!!
     
    dubs89, Dec 1, 2010 IP
  7. Cozmic

    Cozmic Member

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    48
    #7
    Just add the class declaration somewhere above the code. In a global file if possible.

    Then you can text a phone number by storing whatever you want to test in $somePhoneNumber , then testing it with:

    
    $pn = new phoneNumber($somePhoneNumber);
    $result = $pn->get();
    
    Code (markup):
    $result is what you should actually add to your database. $result stores the properly formatted phone number. The script formats all phone numbers the same way so that everything will be consistent.
     
    Cozmic, Dec 1, 2010 IP