I’ve got this regular expression validator that makes sure a user enters a valid phone number. The expression in question is:
(((\d{3}) ?)|(\d{3}-))?\d{3}-\d{4}
Now that allows entry of US phone numbers in the format (123) 456-7890, 123-456-7890, or 123-4567. However, I want to disallow the 7-digit variety, as getting people’s phone numbers without the area code is a might bit useless, but I can’t quite figure out which bit I need to change. What should this expression be?
((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}
^
|
+----- Here
The ? indicated by the arrow makes the preceding expression, which matches the area code in either format, optional. If you remove that question mark, it will be required.
Assuming d stands for any digit in the range 0-9, and D stands for any digit in the range 2-9 (area codes and exchanges can’t start with 0 or 1), you want something like this:
((Ddd) |Ddd-)Ddd-dddd
Translating this expression into your language’s syntax shouldn’t be too hard. I assume that there’s some escape character for the space, but I don’t know it off the top of my head.