Reputation: 2534
I am trying to get a regex that will allow me to validate a phone with a mask.
The mask is () -
Example (333) 444-5555
Rules:
So far this is what I have but it allows phones without parenthesis:
^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$
Upvotes: 0
Views: 134
Reputation: 46871
As per rule it should work
^\(\d{3}\) \d{3}-\d{4}$
You can use [0-9]
instead of \d
as well.
?
means zero and one time
Upvotes: 1
Reputation: 46
Here's my take on it:
^((\(\d{3}\)|\d{3})(\s|-|\.))?\d{3}(\s|-|\.)?\d{4}$
Upvotes: 0