I’m looking for a regex for validating phone numbers.
Here is what I’d like my regex to validate:
- Must be 14 to 17 characters in length
- Must be all digits and contain 3 hyphens
Accepted formats:
5-555-555-5555
55-55-555-5555
55-555-555-5555
555-555-555-5555
5555-555-555-5555
My current code looks like this:
^.*(?=.{14,17})(?=.*\-{3,})(?=.*[\d\-]).*$
It is allowing more than 3 hyphens and the length is being allowed to exceed 17 characters.
Thanks in advance for your help!
Here is an ugly brute force version that simply lists the 4 valid formats explicitly:
Here is the Rubular link.