Re: PHP Phone Number Validation

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

 



On 05/16/2005 7:48 PM, IMEX Research <jeremy.white@xxxxxxxxxxxxxxxx> wrote:

> OK, I know this has probably gone aruond the list a few times, but how do I
> validate a phone number that is in the format ddd-ddd-dddd ??  I can't figure
> out how.

I try to not restrict the way your users enter phone numbers or other types
of structured data. I wouldn't bother validating against ddd-ddd-dddd unless
you also plan on validating (ddd) ddd-dddd, ddd.ddd.dddd, +1 ddd ddd ddd or
any number of variations. Your best bet is to make sure that the number
contains 10 (or 11) digits:

$phone = preg_replace('/[^0-9]/', '', $phone); # remove non-numbers

if (preg_match('/^1?[0-9]{10}$/', $phone)) {
    echo 'Valid';
}

To answer your original question:

if (preg_match('/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/', $phone)) {
    echo 'Valid';
}

Paul

-- 
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