8

What's a good way to validate phone numbers being input in codeigniter?

It's my first time writing an app, and I don't really understand regex at all.

Is it easier to have three input fields for the phone number?

Kara
  • 6,115
  • 16
  • 50
  • 57
Kevin Brown
  • 12,602
  • 34
  • 95
  • 155

4 Answers4

15

Here's a cool regex I found out on the web. It validates a number in almost any US format and converts it to (xxx) xxx-xxxx. I think it's great because then people can enter any 10 digit US phone number using whatever format they are used to using and you get a correctly formatted number out of it.

Here's the whole function you can drop into your MY_form_validation class. I wanted my form to allow empty fields so you'll have to alter it if you want to force a value.

function valid_phone_number_or_empty($value)
{
    $value = trim($value);
    if ($value == '') {
        return TRUE;
    }
    else
    {
        if (preg_match('/^\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/', $value))
        {
            return preg_replace('/^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/', '($1) $2-$3', $value);
        }
        else
        {
            return FALSE;
        }
    }
}
Dana
  • 1,460
  • 3
  • 14
  • 26
  • Create a file in your libraries folder called MY_form_validation. Make sure you're calling $this->load->library('form_validation') in your controller. Then in the function that processes the submission, do $this->form_validation->set_rules('phone_1', 'Phone 1', 'valid_phone_number_or_empty'); Where phone_1 is your form field. – Dana Dec 23 '09 at 14:50
  • instead of using trim you should take the first line of code from Dan Beam (but replace $_POST with $value) and then format it the way you want it. – Dennis Jan 28 '10 at 23:42
3

The best way is not to validate phone numbers at all, unless you're absolutley 100% positive that you're only dealing with phone numbers in the US or at least North America. As soon as you allow phone numbers from Europe I don't think there's a regex which covers all possibilities.

Fant
  • 39
  • 2
  • Yep. This is basically the same with email-addresses, as you can see here: http://www.regular-expressions.info/email.html – Dan Soap Dec 20 '09 at 23:09
  • what's the common thing of all phone numbers? that's right, the **numbers**! see my post below – Dan Beam Dec 20 '09 at 23:18
3

strip out the non-digits with this:

$justNumbers = preg_replace( '/\D/', $_POST[ 'phone_num' ] );

and see if there are enough characters!

$numRequired = 7; // make your constraints here
if( strlen( $justNumbers ) < $numRequired ){ /* ... naughty naughty ... */ }

this is loose, of course, but will work for international numbers as well (as all it's really doing is seeing if there are over 7 numbers in the input).

just change the number of required digits according to your specifications.

Dan Beam
  • 3,632
  • 2
  • 23
  • 27
  • The thing about this is that there are lots of places that use 10 digit phone numbers and some that still use 7. This also doesn't take into effect the use of parenthesis for the area code or hyphens/spaces to separate the pre and post fixes for phone numbers. – DrewRobertson93 Aug 26 '14 at 16:56
0

The regex solution as explained by Dan would be the way to do it - but you might want to re-think validating phone numbers at all. What if the user wants to add a note like he/she would do on paper? - I see many values entered in the phone number fields to be stuff like:

307-555-2323 (home)

or

307-555-3232 after 6pm

I think today we can assume the users knows what to enter.

Steven
  • 113
  • 1
  • 5
  • 7
    There is always good reason to restrict data entry via validation. Fields are not for notes, they are for specific information. – Kevin Brown Jun 03 '10 at 21:02