> -----Original Message-----
> From: IMEX Research [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, May 17, 2005 2:49 AM
> To: php-general@lists.php.net
> Subject: [PHP] PHP Phone Number Validation
> 
> 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.

With regular expressions: 

http://dk.php.net/manual/en/function.ereg.php
http://dk.php.net/manual/en/function.preg-match.php

I expect You wanna match 555-666-0606? (Angelina Jolies number ;-)

This will match the number from start to end:

^[0-9]{3}-[0-9]{3}-[0-9]{5}$

^ means the start of the string
$ means the end of the string
[0-9] means any number from 0 to 9
{3} means _exactly_ 3 occurences, no more no less (can also be {2,4} if
You world allow from 2 to 4 occurences or {2,} if You want at least 2
digits)

So this line says _start_ with 3 digits followed by a - then 3 digits,
then another - and end with 4 digits

Example:

$number = "555-666-0606";

if(ereg("^[0-9]{3}-[0-9]{3}-[0-9]{4}$", $number)) {
  echo "valid phonenumber";
}
else {
  echo "invalid phonenumber";
}

--
Med venlig hilsen / best regards
ComX Networks A/S
Kim Madsen
Systemudvikler/Systemdeveloper

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

Reply via email to