views:

32

answers:

1

Hello,
I have to do phone number validation using JavaScript.
I have already done validation for numbers as follows,

  
var filter =/^[0-9]+$/

But now I have to also allow hyphen and "()".
Please provide me a way for the same.

+5  A: 

How about:

/^[0-9()-]+$/

Notes:

  • Parenthesis have no special meaning in a character set
  • If you start, or end, with a minus-sign, it is not interpreted as a range, but as the minus-character itself
Lasse V. Karlsen