Counting and limiting words in a textarea

前端 未结 6 1688
情歌与酒
情歌与酒 2020-11-28 13:35

I managed to make this little jquery function to count the number of words entered in textarea field.

here is the fiddle

and here is the code:

JQUERY

相关标签:
6条回答
  • 2020-11-28 13:57

    Adding a simple if condition will solve your problem.

    $.each(wordCounts, function(k, v) {
        if(finalCount <= 200) {
            //Todos
        }
        else {
          return false;  //prevent keyup event
        }
     });
    
    0 讨论(0)
  • 2020-11-28 14:02

    I would do it like this ?

    $("#word_count").on('keydown', function(e) {
        var words = $.trim(this.value).length ? this.value.match(/\S+/g).length : 0;
        if (words <= 200) {
            $('#display_count').text(words);
            $('#word_left').text(200-words)
        }else{
            if (e.which !== 8) e.preventDefault();
        }
    });
    

    FIDDLE

    0 讨论(0)
  • 2020-11-28 14:16

    $(document).ready(function(){ $("textarea").on('keyup',function(){

                    var value = $('textarea').val();
                    var wordCount = 0;
    
                    if(value == ""){
                        $('textarea').empty();
                    }
                    else{
    
                        var regex = /\s+/gi;
                        var wordCount = value.trim().replace(regex, ' ').split(' ').length;
                    }
    
                        if(wordCount > 25){
                         var trimmed = $(this).val().split(/\s+/,25).join(" ");
                        $(this).val(trimmed + " ");
                }
                else{
                    $('#display_count').html(25- wordCount +" words left");
                }
    
    
                  });
    

    });

    0 讨论(0)
  • 2020-11-28 14:19

    A simple plugin can be found here:

    Simple Textarea Word Counter using jQuery

    0 讨论(0)
  • 2020-11-28 14:21

    Using return false to stop keyup events doesn't block the event, because in this case the event has already fired. The keyup event fires when the user releases a key, after the default action of that key has been performed.

    You will need to programmatically edit the value of the textarea you have as #wordcount:

    $(document).ready(function() {
      $("#word_count").on('keyup', function() {
        var words = this.value.match(/\S+/g).length;
    
        if (words > 200) {
          // Split the string on first 200 words and rejoin on spaces
          var trimmed = $(this).val().split(/\s+/, 200).join(" ");
          // Add a space at the end to make sure more typing creates new words
          $(this).val(trimmed + " ");
        }
        else {
          $('#display_count').text(words);
          $('#word_left').text(200-words);
        }
      });
    }); 
    

    http://jsfiddle.net/7DT5z/9/

    0 讨论(0)
  • 2020-11-28 14:21

    You can use positive lookahead regexes to preserve the whitespace - so that returncodes and tabs are not collapsed to a single space. Something like this:

      var wordLimit = 5;
      var words = 0;
      var jqContainer = $(".my-container");
      var jqElt = $(".my-textarea");
    
    function charLimit()
    {
      var words = 0;
      var wordmatch = jqElt.val().match(/[^\s]+\s+/g);
      words = wordmatch?wordmatch.length:0;
    
      if (words > wordLimit) {
          var trimmed = jqElt.val().split(/(?=[^\s]\s+)/, wordLimit).join("");
          var lastChar = jqElt.val()[trimmed.length];
          jqElt.val(trimmed + lastChar);
      }
      $('.word-count', jqContainer).text(words);
      $('.words-left', jqContainer).text(Math.max(wordLimit-words, 0));
     }
     
     jqElt.on("keyup", charLimit);
     charLimit();
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <div class="my-container">
      <textarea class="my-textarea"></textarea>
      <span class="words-left"></span> words left
    <div>

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