Search code examples
asp.nettextbox

Asp.net Counting Number of Characters being written in a textbox


I want to be able to display current number of characters, in a asp label, written in a textbox while the user is currently writting in it. I've read partial articles on the web but none of them works.

I've tried using

<asp:TextBox ID="txtInput" runat="server" TextMode="MultiLine" onkeyup="Count(this.id)"></asp:TextBox>

and

<script type="text/javascript">
        function Count(x) {
           document.getElementById(lbNrChar).value = document.getElementById(x).value.length;
        }
</script>

but without any success..


Solution

  • we used Javascript to solve this:

    document.getElementById(value).value.length...

    Note that if the control is a label then you have to use the .innerHTML not .value property for it to take the length.

        document.getElementById("Label1").innerHTML = 
    document.getElementById("TextBox1").value.length; 
    

    vs. if it's another text box you would use .value.

    on the server side you can get it by textbox.text.length