Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Allow Only Numbers/Nothing/Date in textbox on masterpage

0.00/5 (No votes)
7 Jul 2013 1  
This jquery limits the user only entered numbers.

Introduction

This tip discusses how to add only to enter numbers, nothing to enter and date with jquery on all pages in ASP.NET.

Just add the below code on master page. It will work with all pages.

Using the Code

Insert the following code in your masterpage.master:

  <head>   <link href="../css/custom-theme/jquery-ui-1.10.1.custom.css" 
rel="stylesheet" type="text/css" />
    <script src="../js/jquery-1.9.1.js" 
    type="text/javascript">
    </script> <script language="javascript" 
    type="text/javascript">

              $(document).ready(function () {          
                // for numbers only
                  $("input[type='text']").focus(function () {
                      if (this.title == "numbers")
                          $(this).keydown(function (event) {
                              if ((event.keyCode >= 48 && event.keyCode <= 57) || 
                              (event.keyCode >= 96 && event.keyCode <= 105) || 
                              (event.keyCode == 8) || (event.keyCode == 9) || (event.keyCode == 12) || 
                              (event.keyCode == 27) || (event.keyCode == 37) || (event.keyCode == 9) || 
                              (event.keyCode == 39) || (event.keyCode == 46) || (event.keyCode == 190) || 
                              (event.keyCode == 110)) { return true; } else { return false; }
                          });
                  });
   // for to enter nothing
                  $("input[type='text']").focus(function () {
                      if (this.title == "nothing")
                          $(this).keydown(function (event) {
                              if ((event.keyCode == 9)) { return true; } else { return false; }
                          });
                  });
// for to enter date  i-e  (numbers and '/')
                  $("input[type='text']").focus(function () {
                      if (this.title == "date")
                          $(this).keydown(function (event) {
                              if ((event.keyCode >= 48 && event.keyCode <= 57) || 
                              (event.keyCode >= 96 && event.keyCode <= 105) || 
                              (event.keyCode == 8) || (event.keyCode == 9) || (event.keyCode == 12) || 
                              (event.keyCode == 27) || (event.keyCode == 37) || (event.keyCode == 9) || 
                              (event.keyCode == 39) || (event.keyCode == 46) || (event.keyCode == 190) || 
                              (event.keyCode == 110) || (event.keyCode==191)) 
                              { return true; } else { return false; }
                          });
                  });
 } ); </script></head>

To add texbox, just add the tooltip property, i.e., to only allow numbers ToolTip="numbers" on any you don't need to code extra on each to validate:

<asp:TextBox  ToolTip="numbers" ID="txtMale" runat="server"></asp:TextBox>

To enter nothing, just ToolTip="nothing":

<asp:TextBox  ToolTip="nothing" ID="txtFillbysystem" runat="server"></asp:TextBox>

To allow "/" with numbers, ToolTip="date":

<asp:TextBox  ToolTip="date" ID="txtContractDate" runat="server"></asp:TextBox>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here