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.
views:
1126answers:
4A 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.
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]*$
These links should be able to help you How To: Use Regular Expressions to Constrain Input in ASP.NET, Regular Expressions in ASP.NET
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.