admin管理员组

文章数量:1410731

i have a textbox and i want dynamic character count of how many char are left out of total max limit=500.

<asp:TextBox ID="TextBox2" runat="server" TextMode="MultiLine" Width="300" Height="100">asp:TextBox>

i have a textbox and i want dynamic character count of how many char are left out of total max limit=500.

<asp:TextBox ID="TextBox2" runat="server" TextMode="MultiLine" Width="300" Height="100">asp:TextBox>
Share Improve this question asked Jun 12, 2017 at 7:21 user8080469user8080469 0
Add a ment  | 

2 Answers 2

Reset to default 4

You can use val.value.length to get a number of characters in the textbox/textarea.

function countChar(val) {
  var len = val.value.length;
  if (len >= 500) {
    val.value = val.value.substring(0, 500);
  } else {
    $('.numbersofChart').text(500 - len);
  }
};
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="TextBox2" onkeyup="countChar(this)"></textarea>
<div class="numbersofChart"></div>

solved your issue please test this code may help you better

<h5 >Characters Left <span id="remaining">500</span></h5>

<input type="text" id="question_ment_body" name="question_ment_body" placeholder="Your Comment Here...">


<script type="text/javascript">
    function charCount() {
        var textEntered = document.getElementById('question_ment_body').value;
        var msg = document.getElementById('remaining');
        var counter = (500-(textEntered.length));
        msg.textContent = counter;
    }
    var el = document.getElementById('question_ment_body');
    el.addEventListener('keyup',charCount,false);
</script>

本文标签: javascriptDynamic character count of aspnet text box using JSJqueryStack Overflow