How to Count Characters in a Textbox using JavaScript in ASP.NET

Count Characters in a Textbox using JavaScript in ASP.NET

Textbox type the some character now want to restrict the character then using javascript count character remove  the specified characters in Asp.Net.




 <script type="text/javascript">
        function CountRemove() {
            var txt = document.getElementById("TextBox2").value;
            document.getElementById("Label2").innerHTML = document.getElementById("TextBox2").value.length + " Characters";
            if (document.getElementById("TextBox2").value.length >= 10) {
                document.getElementById("TextBox2").value = txt.substring(0, txt.length - 1);
        function Count() {
            document.getElementById("Label1").innerHTML = document.getElementById("TextBox1").value.length + " Characters";




<head runat="server">
    <title>How to Count Characters in a Textbox using JavaScript in ASP.NET</title>
    <script type="text/javascript">
        function CountRemove() {
            var txt = document.getElementById("TextBox2").value;
            document.getElementById("Label2").innerHTML = document.getElementById("TextBox2").value.length + " Characters";
            if (document.getElementById("TextBox2").value.length >= 10) {
                document.getElementById("TextBox2").value = txt.substring(0, txt.length - 1);
        function Count() {
            document.getElementById("Label1").innerHTML = document.getElementById("TextBox1").value.length + " Characters";


    <form id="form1" runat="server">
    <div align="center">
    <asp:TextBox ID="TextBox1" runat="server" onkeyup="Count();">
<asp:Label ID="Label1" runat="server" ForeColor="#CC3300"></asp:Label>
  <asp:TextBox ID="TextBox2" runat="server" placeholder="Allow 10 Characters" onkeyup="CountRemove();">
<asp:Label ID="Label2" runat="server" ForeColor="#FF3399"></asp:Label>




First - Add new webform - place the required textbox and label  - textbox onkeyup=call javascript function

Javascript function  call the textbox value from getElementId - and access from the appropriate textbox length


Post a Comment