Wednesday 22 June 2016

javascript - Validation for xxx-xxx-xxxx or (xxx)xxx-xxxx










Whats the regex for




xxx-xxx-xxxx
or
(xxx)xxx-xxxx


I can create regex for the first one with



/^\d{3}-\d{3}-\d{4}$/



but how to add rule in this so it can also handle the second one?


Answer



You can use this: -



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

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...