Re: Phone number validation

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, Mar 12, 2006 at 12:09:42PM -0700, Paul Goepfert wrote:
> Hi all,
> 
> I am trying to validate phone numbers in a web form that I have
> created.  I am using a regular expression to validate the phone
> number.  It seems when I enter the phone number in the following ways
> I get errors
> 
> (123) 456 7890
> 123 456 7890
> (123) 456 - 7890
> 123 456-7890

Dont forget:

  123.456.780
  1-123-456-7890

> 
> ...
> 
> $validPhone = "^([0-9]{3}[ ]*)?[0-9]{3}[ ]*[0-9]{4}$";
> 
> By the way The phone numbers are in US format.

With US phone numbers I always use this approach to avoid what
format people use to enter the phone:

  - Remove any non digit
     $check_phone = preg_replace('/[^0-9]/', '', $phone);


  - If $check_phone strlen < 10.. invalid
  - if $check_phone strlen == 11 && first char == '1'.. remove it
  - if $check_phone strlen == 10, seems ok
    - possible area code check with prefix check (if have db to
      support that)

I would even go as far as storing the phone striped of any
formating and only format it when you display it.

The other option would to make three seperate input fields in the
form and join them together in your script:

 (<input name="phone[area]">) <input name="phone[code]">-<input name="phone[num]">


Curt.
-- 
cat .signature: No such file or directory

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux