Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validation for xxx-xxx-xxxx or (xxx)xxx-xxxx [duplicate]

Possible Duplicate:
A comprehensive regex for phone number validation

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?

like image 957
coure2011 Avatar asked Oct 12 '25 15:10

coure2011


2 Answers

You can use this: -

/^(?:\(\d{3}\)|\d{3}-)\d{3}-\d{4}$/
like image 184
Rohit Jain Avatar answered Oct 14 '25 05:10

Rohit Jain


I'm no guru but this should do the trick;

/^(\d{3}\-)?(\(\d{3}\))?\d{3}\-\d{4}$/
like image 23
Daniel Avatar answered Oct 14 '25 06:10

Daniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!