|
Posted by Paul Fierro on 05/17/05 17:46
On 05/16/2005 7:48 PM, IMEX Research <jeremy.white@imexresearch.com> 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
Navigation:
[Reply to this message]
|