Re: PHP Phone Number Validation

From: Date: Tue, 17 May 2005 14:46:06 +0000
Subject: Re: PHP Phone Number Validation
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-215397@lists.php.net to get a copy of this message
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

« previous php.general (#215397) next »