Vimal
Vimal

Reputation: 2857

Regular expression for text box validation with alphabets, numbers and some special characters

I am trying to validate a text box using regular expression. I want to add alphabets, numbers and some special characters like "& - / . # , space". I have tried with this

/^[a-zA-Z0-9&-/.#,\s]*$/.

It is working fine except the *. I want to prevent * symbol.

Upvotes: 2

Views: 134

Answers (2)

Jaiprakash Khowal
Jaiprakash Khowal

Reputation: 55

Put & at the end of regex Like below

/^[a-zA-Z0-9-/.#,\s&]*$/

Upvotes: 1

Kobi
Kobi

Reputation: 138007

You should escape the hyphen and slash:

/^[a-zA-Z0-9&\-\/.#,\s]*$/

&-/ is interpreted as a range, just like a-z, which include * and some other characters.
Escaping the slash is not mandatory here, but I always try to escape it in regex literals (/this syntax/).

Upvotes: 1

Related Questions