Introduction
In this article, I explain how to create validations for a Text Box control in ASP.Net. I will start with the following three validations:
- Accept alphanumeric characters
- Accept Alphabet characters
- Accept numeric characters
Requirement
Add the following line in the head section:
- <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
Code
First write the following script in your page to accept alphanumeric characters :
- <script type="text/javascript">
- $(function () {
- $('#txtalphaNumeric').keydown(function (e) {
- if (e.shiftKey || e.ctrlKey || e.altKey) {
- e.preventDefault();
- } else {
- var key = e.keyCode;
- if (!((key == 8) || (key == 32) || (key == 46) || (key >= 35 && key <= 40) || (key >= 65 && key <= 90) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105))) {
- e.preventDefault();
- }
- }
- });
- });
When you have done that code, add a TextBox and provide "txtalphaNumeric" for the id of the TextBox like :
- <asp:TextBox ID="txtalphaNumeric" runat="server"></asp:TextBox>
2. Accept Alphabet characters
First write the following script in your page :
- $(function () {
- $('#txtalphabet').keydown(function (e) {
- if (e.shiftKey || e.ctrlKey || e.altKey) {
- e.preventDefault();
- } else {
- var key = e.keyCode;
- if (!((key == 8) || (key == 32) || (key == 46) || (key >= 35 && key <= 40) || (key >= 65 && key <= 90))) {
- e.preventDefault();
- }
- }
- });
- });
When you have done that code, add a TextBox and provide "txtalphabet" for the id of the TextBox like:
- <asp:TextBox ID="txtalphabet" runat="server"></asp:TextBox>
3. Accept numeric characters
First write the following script in your page :
- $(function () {
- $('#txtNumeric').keydown(function (e) {
- if (e.shiftKey || e.ctrlKey || e.altKey) {
- e.preventDefault();
- } else {
- var key = e.keyCode;
- if (!((key == 8) || (key == 46) || (key >= 35 && key <= 40) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105))) {
- e.preventDefault();
- }
- }
- });
- });
After you have done that code, add a TextBox and provide "txtalphabet" for the id of the TextBox like:
- <asp:TextBox ID="txtNumeric" runat="server"></asp:TextBox>
Complete code
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>jQuery Allow only alphanumeric characters in TextBox</title>
- <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
- <script type="text/javascript">
- $(function () {
- $('#txtalphaNumeric').keydown(function (e) {
- if (e.shiftKey || e.ctrlKey || e.altKey) {
- e.preventDefault();
- } else {
- var key = e.keyCode;
- if (!((key == 8) || (key == 32) || (key == 46) || (key >= 35 && key <= 40) || (key >= 65 && key <= 90) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105))) {
- e.preventDefault();
- }
- }
- });
- });
- //only alphabet
- $(function () {
- $('#txtalphabet').keydown(function (e) {
- if (e.shiftKey || e.ctrlKey || e.altKey) {
- e.preventDefault();
- } else {
- var key = e.keyCode;
- if (!((key == 8) || (key == 32) || (key == 46) || (key >= 35 && key <= 40) || (key >= 65 && key <= 90))) {
- e.preventDefault();
- }
- }
- });
- });
- //Only Numeric
- $(function () {
- $('#txtNumeric').keydown(function (e) {
- if (e.shiftKey || e.ctrlKey || e.altKey) {
- e.preventDefault();
- } else {
- var key = e.keyCode;
- if (!((key == 8) || (key == 46) || (key >= 35 && key <= 40) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105))) {
- e.preventDefault();
- }
- }
- });
- });
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <b>Enter Text:</b><asp:TextBox ID="txtalphaNumeric" runat="server"></asp:TextBox>Accept alphanumeric characters<b><br />
- Enter Text:</b><asp:TextBox ID="txtalphabet" runat="server"></asp:TextBox>
- Accept only Alphabets characters
- Enter text :<asp:TextBox ID="txtNumeric" runat="server"></asp:TextBox>Accept only Numeric values\
- </div>
- </form>
- </body>
- </html>

Hamid KhanPosted Mar 24, 2021, 9:06 AM
Nice article
Saravanakumar SekaranPosted Oct 29, 2014, 10:42 PM
very nice
Former memberPosted Jun 15, 2014, 5:30 AM
Very nice article
GokulPosted Jul 25, 2013, 9:36 PM
Hai Neha, Its good and simple artical to understand textbox validation. But Its very old methodalogy, Give us some new way to do the validation. Thanks
Neha SharmaPosted May 28, 2013, 7:26 AM
thx everyone read the next part here : http://www.c-sharpcorner.com/UploadFile/9f0ae2/Asp-Net-text-box-validation-part-ii/
Goutam YadavPosted May 28, 2013, 2:49 AM
good, thanks
Amirhossein ParsaeeyanPosted May 21, 2013, 4:40 PM
thank's a lot
Sreejesh SPPosted May 21, 2013, 2:55 AM
nice, Dear neha please give me text box date format validation
Ravikumar GPosted May 21, 2013, 2:45 AM
Nice...