Welcome Guest, Not a member yet? Register   Sign In
UK Phone Number Validation (regexp trickle!)
#1

[eluser]Unknown[/eluser]
I just spent a couple of minutes writing a UK phone number validation callback for CI. I looked around and couldn't find one (there are some for the US dotted about -- good luck US people, those numbers look slippery!) so used some javascript from here:

http://www.braemoor.co.uk/software/telnumbers.shtml

I've kept commented out code in there, to help anyone who wants to extend the scope of the callback, though it's probably unnecessary because you can get CI to do most of the work for you.

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

Code:
//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:

Code:
/**
    * 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> --&gt; ' . $errorarr[$telnum_error]);
            return FALSE;
        }else    {
            return TRUE;
        }
    }

Not at all elegant in that it's just a nested if tree (vomit vomit), but it should be useful to extend existing functions. Feel free to comment on ways to improve it and I'll refactor and repost.


Messages In This Thread
UK Phone Number Validation (regexp trickle!) - by El Forum - 02-03-2011, 07:09 AM
UK Phone Number Validation (regexp trickle!) - by El Forum - 07-07-2012, 06:52 PM



Theme © iAndrew 2016 - Forum software by © MyBB