-1

I am new to RegEx and need some guidance. Right now I have the following validation for phone numbers:

(\d{3}) ?\d{3}( |-)?\d{4}|^\d{3}( |-)?\d{3}( |-)?\d{4}

Unfortunately, the system I am importing the results into does not think favorably of the numbers being separated solely by spaces or not separated at all. What would the formula look like that requires either dashes or parentheses and accepts only the following formats: XXX-XXX-XXXX or (XXX) XXX-XXXX?

Thank you for your assistance.

1 Answers1

0

Start simple:

\d{3}-\d{3}-\d{4} works beautifully for numbers like 212-867-5309.

As for others, I'd say you and your users would be better off if you kept it simple. No switching, no choices. Pick a standard. Simple is good.

If you must persist, look at this web site for help. You aren't the first.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Thank you so much for answering. Our database puts the initial output in the (XXX) XXX-XXXX format, so that has to be permitted, but it is a pain in the backside for people to duplicate. That is why we were looking to also allow the XXX-XXX-XXXX formatting as well. – TammyLuck Jun 22 '15 at 21:02