0

I want to allow only the following characters in a string: digits, parentheses and the plus sign, which is [0-9] ( ) +

I can't seem to get a combination to get the validator to return true, the only option seems to be an list of every other possible character that's NOT allowed... which would make for a large list!

Am I missing something?

1
  • Can you please show us what you tried so far? Commented Aug 20, 2011 at 16:40

2 Answers 2

1

This should work:

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

The regex I gave you there also accepts the empty string. If you want to disallow empty then change the * near the end to a +.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to slash-escape parens and the plus, because they have special meanings in regex:

/^[\d\(\)\+]+$/

1 Comment

I knew to escape the parens and plus... I was missing the ^ and $ from the beginning and end, I didn't know why they were needed. Thanks.

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.