Click here to Skip to main content
15,920,383 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
In my ongoing project I am held up at a multiline textbox where I have fixed the character length to 400. I want the length to reduce when any keyboard character entered and the length increases when any character deleted. The counter shall be displayed in a label. Can any one help?
Posted

Hello, yes you can do this with javascript. please follow the sample code:


XML
<form id="form1" runat="server">
    <script type="text/javascript">
        function CountChar(idTxtBox) {


            document.getElementById('lblCountChar').innerHTML = document.getElementById(idTxtBox).value.length;
        }
    </script>
    <asp:TextBox ID="txtDetails" Width="200" runat="server" TextMode="MultiLine" onkeyup="CountChar(this.id);" />
    <asp:Label ID="lblCountChar" Text="" runat="server" />

    </form>
 
Share this answer
 
You may add a handler for the TextChanged event, this way:
C#
private void myTextBox_TextChanged(object sender, EventArgs e)
{
  int remain = 400 - myTextBox.Text.Length;
  myLabel.Text = remain.ToString();
}
 
Share this answer
 
Hi,

Use the following,

XML
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
 <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">
        function stringCounter(textBox, countlabel, maxlength) {
            countlabel.value = maxlength - textBox.value.length;
        }
    </script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

   <asp:TextBox ID="TextBox1" runat="server" Height="100" Width="550" Wrap="true"
           TextMode="MultiLine" MaxLength="500" onkeyup="stringCounter(MainContent_TextBox1, MainContent_label1, 500);"
           onkeydown="stringCounter(MainContent_TextBox1, MainContent_label1, 500);" />
&nbsp;&nbsp;
   <asp:TextBox ID="label1" runat="server" ReadOnly="true" BorderStyle="None"></asp:TextBox>
</asp:Content>
 
Share this answer
 
Comments
S.Rajendran from Coimbatore 21-Dec-12 4:49am    
Thanks Sampath. I will try and come back to you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900