Dave
Dave

Reputation: 783

jQuery - Regex to prevent any special characters

I'm using jQuery's validation plugin to validate a form. I have a field which should only contain letters or numbers. I have tried the following regex but it's still allowing characters such as ' ; :

[a-zA-Z 0-9]$

What am I doing wrong?

Thanks in advance

Upvotes: 1

Views: 5022

Answers (2)

limc
limc

Reputation: 40170

Try this:

^[a-zA-Z 0-9]*$

If this is a required field, then change asterisk to plus:-

^[a-zA-Z 0-9]+$

Upvotes: 3

Norse
Norse

Reputation: 5757

Try this expression::

^[a-zA-Z 0-9]+$

Upvotes: 1

Related Questions