Anu
Anu

Reputation: 35

Regex for numbers and specific special characters

I am using this regex for validating numbers and specific set of special character.

([0-9]+[ \(\)-/#]*)$

This does not restrict special characters as specified. What is the problem with my regex.

thanks..

Upvotes: 1

Views: 122

Answers (1)

anubhava
anubhava

Reputation: 785038

Make sure to use start anchor ^ and avoid unnecessary escaping inside character class:

^([0-9]+[ ()/#-]*)$
  • Start/end anchors will avoid the problem of matching unwanted input.
  • Also note that hyphen avoids escaping if it is placed at first or last position inside character class

Upvotes: 1

Related Questions