Max characters in textarea with jquery

前端 未结 16 1670
逝去的感伤
逝去的感伤 2020-12-03 04:53

I have the following code, and I\'m kind of stuck on what to do next. The idea is when you enter text into a text area a counter tells you how many characters you have left.

相关标签:
16条回答
  • 2020-12-03 05:25

    I find this works perfectly, and doesn't even flash the excess characters in the textarea before removing them, as a few other solutions do.

    <h1>Add a comment:</h1>
    <form>
        <textarea name="commentText" id="commentText"></textarea>
    </form>
    <br><span id="commentChars">1000</span> characters remaining
    
    <script type="text/javascript">
    
        function checkChars(){
            var numChars = $('#commentText').val().length;
            var maxChars = 1000;
            var remChars = maxChars - numChars;
            if (remChars < 0) {
                $('#commentText').val($('#commentText').val().substring(0, maxChars));
                    remChars = 0;
                }
            $('#commentChars').text(remChars);
        }
    
        $('#commentText').bind('input propertychange', function(){
            checkChars();
        });
    
        checkChars();
    
    </script>
    
    0 讨论(0)
  • 2020-12-03 05:28

    ehm, textarea maxlength is a valid attribute in html5 ? not supported in ie9, thats all.

    w3nerds http://www.w3.org/TR/html-markup/textarea.html#textarea.attrs.maxlength

    w3fools http://www.w3schools.com/tags/att_textarea_maxlength.asp

    0 讨论(0)
  • 2020-12-03 05:30

    I think best way using maxlenght property + jquery keydown, keyup, paste events

    // text input maximum lenght
    $(document).on('keydown keyup paste', 'input[type=text]', function(e) {
      var textArea = $('input[type=text]').val(),
        textLenght = textArea.length,
        limit = $('textarea').attr('maxlength'),
        remain = 'left ' + parseInt(limit - textLenght) + ' chars';
      $('#remain-title').text(remain);
      if (textLenght > 500) {
        $('textarea').val((textArea).substring(0, limit))
      }
    });
    
    // tex area maximum lenght
    $(document).on('keydown keyup paste', 'textarea', function(e) {
      var textArea = $('textarea').val(),
        textLenght = textArea.length,
        limit = $('textarea').attr('maxlength'),
        remain = 'left ' + parseInt(limit - textLenght) + ' chars';
      $('#remain-description').text(remain);
      if (textLenght > 500) {
        $('textarea').val((textArea).substring(0, limit))
      }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div>
      <label>Title</label>
      <p id="remain-title"></p>
      <input type="text" placeholder="Enter Title" maxlength="500"/>
    </div>
    <br>
    <hr>
    <br>
    <div>
      <label>Description</label>
      <p id="remain-description"></p>
      <textarea placeholder="Enter custom description" rows="4" maxlength="500"></textarea>
    </div>

    See JSFiddle

    0 讨论(0)
  • 2020-12-03 05:34

    Returning false and using .keypress() instead of .keyup() stops input once the length has been reached. Here's the example in a jsFiddle:

    http://jsfiddle.net/p43BH/1/

    Updated to allow backspace.

    0 讨论(0)
提交回复
热议问题