2


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.

1 Answer 1

6

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.