Allow only Alpha Numeric values in a textbox using ASP.Net - Beginner Asp.Net Tutorials.



Here i will explain, how to allow user to enter only Alpha-Numeric Values in a TextBox of ASP.Net. i.e. ABC123.


In the Previous articles i explained how to backup and restore access database using c#.nethow to backup and restore access database using c#.nethow to fill dataset using c#.nethow to pick data from dataset using, how to send value from one form to another using and many more about C#.Net.

Step 1.

Drop a TextBox on your page.

<asp:TextBox ID="textBox1" runat="server"></asp:TextBox>

Step 2.

Drop a RegularExpressionValidator on your page.

And Set Properties as Below :

1. ControlToValidate = textBox1.

2. Error Message = Only Apha-Numeric Values are allowed. (This is the message which will be shown when user enters values other than alpha-numeric.)

3. ValidationExpression = ^[a-zA-Z0-9\s]*$    (This is the Expression Value for only allowing Alpha-Numeric Values. This will allow values like a-z , A-Z, 0-9 and also a space Bar ).

<asp:RegularExpressionValidator id="RegularExpressionValidator1" 
                                                    runat="server" ControlToValidate="textBox1" 
                                                    ValidationExpression="^[a-zA-Z0-9\s]*$" ErrorMessage="Only AlphaNumeric Values are allowed." 

You are Done. Debug Your Application and try to enter Non Alpha-Numeric Values. You will get Error Message like "Only Alpha Numeric values are allowed".


Give your Valuable Comments.
comments powered by Disqus
Follow me on twitter
About Me
Harpreet Singh
I began programming with C++ when i was 17. Then at the middle of my study .NET came. Then I began to read C# and VB.NET. By the time i learnt SQL and ASP.NET, and developed some websites such as news portals that are active now. Currently i am running my own Software company.
More Articles