views:

1126

answers:

4

I want to validate a text box that doesn't accept any special characters using regular expressions. It just takes letters and numbers from 0 to 9. Please provide me the correct regex.

+4  A: 

A regular expression would be [a-zA-Z0-9]* for a box that could be empty or [a-zA-Z0-9]+ for a box that must have at least one character in it. If you have a minimum and maximum length, you can do something more like [a-zA-Z0-9]{m,n} where m is the minimum length and n is the maximum length and if you only had a minimum length, the regex would look more like [a-zA-Z0-9]{m,} where m was the minimum number of characters.

For more information, you might want to read this MSDN article on Regular Expressions in ASP.NET.

Thomas Owens
+1, correct, except if it's on the server side in asp.net (VB or C# at least) it wouldn't use the leading and trailing / characters.
John M Gant
I'll edit. I've been doing too much Perl recently.
Thomas Owens
Thomas, this assumes English alphabet only.
Daren Thomas
Yes, it does assume English alphabet. But there's no suggestion that it's not English, either.
Thomas Owens
+1  A: 

This should do it:

^\w+$ or ^\w*$

This matches all letters (upper and lower), numbers and underscores.

If you don't want to match underscores try:

^[a-zA-Z\d]+$ or ^[a-zA-Z\d]*$

David Murdoch
I have applid this expression in ASP.NET, but it failed. Can you help how to apply regular expression validator in asp.net?
Nadeem
Thanx a lot.Now it's working.Thnx again for your help
Nadeem
+1  A: 

These links should be able to help you How To: Use Regular Expressions to Constrain Input in ASP.NET, Regular Expressions in ASP.NET

ntownsend
A: 

While the other responses are accurate in the pattern you'd need, doing a search or browsing at sites like http://regexlib.com would also provide you a good resource for RegEx patterns in the future.

JamesEggers