Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to allow only digits, hypens, space, parentheses and should end with a digit (javascript) [duplicate]

Possible Duplicate:
US Phone Number Verification

I need to validate US phone number. It could be in the format:

xxx-xxx-xxxx
(xxx) xxx xxxx
(xxx)-xxx-xxxx
xxxxxxxxxx

but it should not be

xxx-xxx-xxxx-
-xxx-xxx-xxxx

It should accept digits, hyphens, space and parentheses.

Currently I use

^\[0-9 \-\. ]+$ 

which does not validate dash at the beginning or end.

like image 225
Sandeep Avatar asked Dec 08 '22 21:12

Sandeep


2 Answers

^\(?\d{3}\)?[- ]?\d{3}[- ]?\d{4}$
like image 105
staafl Avatar answered Dec 28 '22 09:12

staafl


Well my idea is (after some searching) not new at all! Look at this:

A comprehensive regex for phone number validation

This is an Excellent suggestion btw.

like image 35
Eugene Avatar answered Dec 28 '22 09:12

Eugene