Count the maximum length of textbox using Javascript

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="MexLength.aspx.cs" Inherits="MexLength" %>  
  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>Maximum length counter</title>  
  6.         <script language="JavaScript">  
  7.             function textCounter(field, countfield, maxlimit) {  
  8.             if (field.value.length > maxlimit) // if too long...trim it!  
  9.             field.value = field.value.substring(0, maxlimit);  
  10.             else  
  11.             countfield.value = maxlimit - field.value.length;  
  12.             }  
  13.         </script>  
  14.     </head>  
  15.     <body>  
  16.         <form id="Form1" name="myform" runat="server">  
  17.             <asp:TextBox ID="TextBoxMessage" runat="server" TextMode="MultiLine" Height="60px" onkeydown="textCounter(TextBoxMessage,LengthCount,25)" onkeyup="textCounter(TextBoxMessage,LengthCount,25)" ></asp:TextBox>  
  18.             <input readonly type="text" name="LengthCount" size="3" maxlength="3" value="25">  
  19.         </form>  
  20.     </body>  
  21. </html>