Click to See Complete Forum and Search --> : REGEX Phone Number Pattern Match


kouki0196
12-16-2004, 03:42 PM
Can someone help me with my REGEX below? It's not meeting my criteria. I want to match the following strings:

123-123-1234
123-123-1234x12345
123-123-1234X12345
123-123-1234 x12345
123-123-1234 X12345
123-123-1234 ext12345


if (!eregi("^[[:digit:].' - ]{10,20}$", stripslashes(trim($_POST['phone'])))) {


Thanks in advance!

Jona
12-16-2004, 03:56 PM
I only like the Perl-compatible RegExes, so if that will work, try the following:


if (!preg_match("/^\d{3}\-\d{3}\-\d{4}(?:\s*?(x|ext)\d{5})?$/i", stripslashes(trim($_POST['phone'])))) {

ShrineDesigns
12-16-2004, 05:09 PM
fyi, phone number extensions can be as short as one digitif(!preg_match("/^\\d{3}\\-\\d{3}\\-\\d{4}(?:\\s*?(x|ext)\\d{1,5})?$/i", stripslashes(trim($_POST['phone']))))
{
// invalid phone number
}

Jona
12-16-2004, 05:21 PM
Originally posted by ShrineDesigns
fyi, phone number extensions can be as short as one digit

I didn't know exactly if kouki wanted that or not, so I just gave exactly what was asked for; thanks, though. :)

kouki0196
12-27-2004, 05:49 PM
I forgot to say "thank you"...

Thank you!
and Merry Christmas too :)

Jona
12-28-2004, 12:37 AM
Merry Christmas. :)