What’s past is prologue

Quote

Whereof what’s past is prologue, what to come / In yours and my discharge.

(The Tempest, Act 2 Scene 1)

Advertisements

UK Phone Number Validation in CodeIgniter

I was looking for a way to validate UK phone numbers in CodeIgniter (or more generally, PHP) and found this on the CodeIgniter forums. Works a treat!

UK Phone Number Validation (regexp trickle!).

For link independence, I’ve copied the pertinent parts from the above link:

So you’d have in your form controller something like:

//Set rules that require numeric only, min length of 9 and field as required.
$this->form_validation->set_rules('phone', 'Phone Number', 'required|numeric|min_length[9]|callback_uk_phone_check');

Then the callback function is:

/**
    * Super quick UK Phone number checker.
    * Adapted from javascript solution found here:
    * http://www.braemoor.co.uk/software/_private/jstelnumbers.js
    **/
    function uk_phone_check($number)
    {
        $errorarr = array(
            //0 => "Valid UK telephone number",
            //1 => "Telephone number not provided",
            //2 => "Please do not include the country code in the telelphone number",
            //3 => "UK telephone numbers should contain 10 or 11 digits",
            4 => "The telephone number should start with a 0",
            5 => "The telephone number is either invalid or inappropriate"
        );
        
        //Set the error to false, our final bit of logic will check against this
        $telnum_error = false;
        
        // Don't allow country codes to be included (assumes a leading "+")
        //$leading = '/^(\+)[\s]*(.*)$/';
        
        // Check that the first digit is 0
        $first = '/^0[0-9]{9,10}$/';
        
        //Check that the telephone number is valid.
        $valid = '/^(01|02|03|05|070|071|072|073|074|075|07624|077|078|079)[0-9]+$/';
        
        // Array holds the regular expressions for the drama telephone numbers
          // see http://stakeholders.ofcom.org.uk/telecoms/numbering/guidance-tele-no/numbers-for-drama
        $drama = array();

        array_push($drama, '/^(0113|0114|0115|0116|0117|0118|0121|0131|0141|0151|0161)(4960)[0-9]{3}$/',
        '/^02079460[0-9]{3}$/', '/^01914980[0-9]{3}$/','/^02890180[0-9]{3}$/','/^02920180[0-9]{3}$/',
        '/^01632960[0-9]{3}$/','/^07700900[0-9]{3}$/','/^08081570[0-9]{3}$/','/^09098790[0-9]{3}$/',
        '/^03069990[0-9]{3}$/');

        //if (preg_match($leading, $number)) {
            //$telnum_error = 2;
        //} elseif (!preg_match($first, $number)) {
        if (!preg_match($first, $number)) {
            $telnum_error = 4;
        } elseif ( !preg_match($valid, $number) ) {
             $telnum_error = 5;
        } else{        
        
        //Check against the drama numbers
            for ($i=0; $i < count($drama); $i++) {
                if ( preg_match($drama[$i], $number) ) {
                    $telnum_error = 5;
                }
            }
        }

        //See if we've got an error
        if($telnum_error){
            //echo "error >>>>>>>>>>>>>>> " . $errorarr[$telnum_error] . " <<<<<<<<<<<<<<<";
            $this->form_validation->set_message('uk_phone_check', 'The %s number must be a valid UK telephone number<br/><u>' .$number. '</u> --> ' . $errorarr[$telnum_error]);
            return FALSE;
        }else    {
            return TRUE;
        }
    }