Max characters in textarea with jquery

前端 未结 16 1668
逝去的感伤
逝去的感伤 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:10

    All of these answers are a bit odd in that they try to do a little too much. A simpler and visually more pleasing way (because it shows the text quickly being cut off) - and with with less oddities that the previous example (note how it overwrites the final key?) - is to simply cut off the number of characters on keyUp to the number that's allowed.

    var maxchars = 400;
    
    $('body').on('keyup paste', 'textarea', function () {
        $(this).val($(this).val().substring(0, maxchars));
        var tlength = $(this).val().length;
        remain = maxchars - parseInt(tlength);
        $('#remain').text(remain);
    });
    

    Note that this then also works for pasting in text, as some of the examples above don't.

    Example here: http://jsfiddle.net/PzESw/5/

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

    A simple way to do this is to set the text in the textarea to a substring of the full amount. You can find an example here:

    http://www.mediacollege.com/internet/javascript/form/limit-characters.html

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

    I know this is a little late, but I just had to figure this out, too. I had to get it to work with UTF8 byte-lengths, and allow certain keypresses. Here's what I came up with:

    checkContent = function (event) {
    
        var allowedKeys = [8,46,35,36,37,38,39,40];
        var contentLength = lengthInUtf8Bytes(jQuery(event.currentTarget).val());
        var charLength = lengthInUtf8Bytes(String.fromCharCode(event.charCode));
    
        if (charLength + contentLength > 20) {
    
            if(jQuery.inArray(event.keyCode, allowedKeys) == -1) {
                event.preventDefault();
            } 
        }
    }
    
    countLength = function(event) {
    
        var len = lengthInUtf8Bytes(jQuery(event.currentTarget).val());
        jQuery('#length').html(len);
    }
    
    
    lengthInUtf8Bytes = function(str) {
        var m = encodeURIComponent(str).match(/%[89ABab]/g);
        return str.length + (m ? m.length : 0);
    }
    
    jQuery(function(){jQuery("#textarea").keypress(function(event){checkContent(event)}).keyup(function(event){countLength(event)})});
    

    You need to have a textarea with id #textarea and an element to display the current length with id #length.

    The keypress event determines whether or not to allow the keypress. The keyup event counts the size of the data in the field after the keypress is allowed/prevented.

    This code works with the following keys: home, end, pagedown, pageup, backspace, delete, up, down, left and right. It doesn't deal with pasting from the clipboard.

    Hope someone finds it useful!

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

    In 2020 in most browsers limiting characters in textarea is supported by one helpful attribute maxlength check at w3chools

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

    Try this. Even works for copy paste from Keyboard:

    $("#comments").unbind('keyup').keyup(function(e){
        var val = $(this).val();
        var maxLength = 1000;
        $('#comments').val((val).substring(0, maxLength));
        $("#comments_length").text($("#comments").val().length);
    });
    
    0 讨论(0)
  • 2020-12-03 05:19
    $("#message-field").on("keyup paste", function() {
      var $this, limit, val;
      limit = 140;
      $this = $(this);
      val = $this.val();
      $this.val(val.substring(0, limit));
      return $('#character-remaining').text(limit - $this.val().length);
    });
    
    0 讨论(0)
提交回复
热议问题