David John
David John

Reputation: 493

Regular Expression Validator for Letters and Numbers only

What is the Regular Expression Validator for only Letters and Numbers in asp.net?

I need to enter only 0-9,a-z and A-Z. I don't want to allow any special characters single or double quotes etc. I am using asp.net 3.5 framework.

I tried ^[a-zA-Z0-9]+$ and ^[a-zA-Z0-9]*$. They are not working.

Any help will be appreciated.

Upvotes: 8

Views: 72042

Answers (4)

Try the following.

^[a-zA-Z0-9]+$

go to this example and also alphanumerics for more

then try this

^[a-zA-Z0-9]*$

If length restriction is necessary use

^[a-zA-Z0-9]{0,50}$

This will match alphanumeric strings of 0 to 50 chars.

Upvotes: 20

Alex from Jitbit
Alex from Jitbit

Reputation: 60566

Dear English speaking people. With all due respect. A-Z are not the only letters in the world. Please use \w instead of [A-Za-z0-9] if you support other languages in your apps

Upvotes: 0

painotpi
painotpi

Reputation: 6996

You can define a regular expression as follows,

Regex myRegularExpression = new Regex(" \b^[a-zA-Z0-9]+$\b");

be sure to include System.Text.RegularExpression

and then use the Regex to match it with your user-control as follows,

eg : if your user-control is a textbox

myRegularExpression.isMatch(myTextBox.Text);

Upvotes: 0

Glory Raj
Glory Raj

Reputation: 17691

you can try this....

 ^[a-zA-Z0-9]+$

see more info at here

Upvotes: 2

Related Questions