Reply to Re: [PHP] PHP Phone Number Validation

Your name:

Reply:


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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация