On Tue, 19 Mar 2002, Kris Vose wrote: > How would you write a regular expression that defines a phone number: ex. >(123)123-1234. > > In other words how would you check to see if there were three numerics surrounded by >(), then three numerics with a "-", then four numerics. > > > This is what I have so far as a regular expression. > > ^[(000-999)]+[000-999\-]+[0000-9999]+$ > > I am using this in an if statement like this: > > If (!eregi("^[(000-999)]+[000-999\-]+[0000-9999]+$", $PHONENUMBER) > { > echo "This phone number is not valid"; > }
Aside from the fact that your regexp isn't valid (look for a little 'n' in the documentation), you might want to consider just stripping the number down to digits (ereg_replace('[^0-9]', '', $number)) and then testing to make sure that has 10 digits (11 if the first figit is a 1) and doesn't begin with 0. Why force people to use your particular punctuation scheme? Some people write: 1.202.555.1212 (202) 555-1212 202/555-1212 202-555-1212 2025551212 There's no point in making them jump through hoops just to save 2 lines of code. Sites that enforce silly things like that really turn me off. You can always impose your preferred formatting after verifying the raw number's validity. miguel -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php