In this blog, I am going to show how we can limit the number of character in a text box and show how many character are left to type in ASP.NET c# using jQuery.

See given below code:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>Limit & Show Character Length</title>
  6. <script src="jquery-1.8.2.js" type="text/javascript"></script>
  7. <script type='text/javascript'>
  8. $(function() {
  9. // Checking Address Length
  10. $('#txtAddress').keyup(function() {
  11. var Address = $('#txtAddress').val();
  12. var Addresslen = Address.length;
  13. if (Address.length >= 250) {
  14. this.value = this.value.substring(0, 250);
  15. }
  16. $('#spanAddress').text(250 - Addresslen + ' Characters Left');
  17. });
  18. //Checking Description Length
  19. $('#txtDescription').keyup(function() {
  20. var Description = $('#txtDescription').val();
  21. var Descriptionlen = Description.length;
  22. if (Description.length >= 500) {
  23. this.value = this.value.substring(0, 500);
  24. }
  25. $('#spanDescription').text(500 - Descriptionlen + ' Characters Left');
  26. });
  27. });
  28. </script>
  29. </head>
  30. <body>
  31. <form id="form1" runat="server">
  32. <div>
  33. <table cellpadding="5" cellspacing="5" width="90%" align="center" style="background-color: SkyBlue;">
  34. <tr>
  35. <td align="center" colspan="2" style="background-color: Green; font-weight: bold;
  36. font-size: 14pt; color: Blue; font-family: Verdana;">
  37. Registration Form
  38. </td>
  39. </tr>
  40. <tr>
  41. <td align="right" width="40%">
  42. Name #:
  43. </td>
  44. <td>
  45. <asp:TextBox ID="txtName" runat="server" Width="250px"></asp:TextBox>
  46. </td>
  47. </tr>
  48. <tr>
  49. <td align="right">
  50. Email #:
  51. </td>
  52. <td>
  53. <asp:TextBox ID="txtEmail" runat="server" Width="250px"></asp:TextBox>
  54. </td>
  55. </tr>
  56. <tr>
  57. <td align="right">
  58. Address:
  59. </td>
  60. <td style="color: Red; font-family: Verdana; font-size: 10pt;">
  61. <asp:TextBox ID="txtAddress" runat="server" TextMode="MultiLine" Height="70px" Width="250px"></asp:TextBox><br />
  62. (<span id="spanAddress">250 Characters left</span>)
  63. </td>
  64. </tr>
  65. <tr>
  66. <td align="right">
  67. About Your Self #:
  68. </td>
  69. <td style="color: Red; font-family: Verdana; font-size: 10pt;">
  70. <asp:TextBox ID="txtDescription" runat="server" TextMode="MultiLine" Height="70px"
  71. Width="250px"></asp:TextBox><br />
  72. (<span id="spanDescription">500 Characters left</span>)
  73. </td>
  74. </tr>
  75. <tr>
  76. <td>
  77. </td>
  78. <td>
  79. <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
  80. </td>
  81. </tr>
  82. </table>
  83. </div>
  84. </form>
  85. </body>
  86. </html>
Now Run the Application.

registration form
Image 1