-1

I have the following phone numbers and I need a regex that validate only numbers with format xxx-xxx-xxxx

281-388-0388   OK
281 388 0388   NO OK
(281)-388-0388 NO OK
(281)-388-0388 NO OK

I have this regex \(?\d{3}\)?-? *\d{3}-? *-?\d{4} but all the above four number will be valid.

Bug
  • 832
  • 2
  • 9
  • 37

2 Answers2

0

You want this as a regex ^[0-9]{3}-[0-9]{3}-[0-9]{4}$

JGNI
  • 3,933
  • 11
  • 21
0

Why not simply the following:

^\d{3}-\d{3}-\d{4}$
Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61