Tuesday, September 24, 2013

How To Validate TextBox using jQuery Block Unwanted Characters

Hello,
         In jQuery you might want to limit the user to type either only numbers or characters. We can do this with jQuery very easily.

1. First create a TextBox set it's id="TextBox1"



   1:  <body>
   2:      <form id="form1" runat="server">
   3:      <div>
   4:          <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
   5:      </div>
   6:      </form>
   7:  </body>

2. Now we will write jQuery to stop user from entering numbers 0-9, i.e. user is only allowed to type Alphabets and some special characters



   1:      <script type="text/jscript">
   2:          $(document).ready(function () {
   3:              $("#TextBox1").keypress(function (e) {
   4:                  if (e.key >= 0 && e.key <= 9)
   5:                      return false;
   6:              });
   7:          });
   8:      </script>

We limit the user from entering numbers using keypress event

I hope it was informative for you and I would like to Thank you for reading

No comments:

Post a Comment