This example shows how to limit the number of characters to be typed into the TextBox using JavaScript and display the remaining characters in a Label Control.

<script type="text/javascript" language="javascript">
   
     function validatelimit(obj, maxchar)
     {
   
        if(this.id) obj = this;
       
        var remaningChar = maxchar - obj.value.length;
        document.getElementById('<%= Label1.ClientID %>').innerHTML = remaningChar;

            if( remaningChar <= 0)
            {
               obj.value = obj.value.substring(maxchar,0);
               alert('Character Limit exceeds!');
               return false;

            }
            else
            {return true;}
     }

    </script>

<body  runat="server">
    <form id="form1" runat="server">

        <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" onkeyup="validatelimit(this,300)">   </asp:TextBox>
        <asp:Label ID="Label1" runat="server" Text="0"></asp:Label>

   </form> 

</body>

Note: 300 is the MAX length of the characters in the TextBox..Just change the value of 300 based on your requirements..

Technorati Tags: ,,