title: How can I limit words, not characters, in textarea HTML
body: <p>I need to control a textarea with 2 clauses, 200 characters and 20 words at most.</p>
  
  <p>I solved the 200 character limit with a maxlength in the HTML, but I don't know how to limit the text by words.</p>
  
  <p>The code is the following:</p>
  
  <pre><code>&lt;textarea onkeydown="get_textvalue();" onkeyup="ad_writer();" id="mytextarea" name="mytextarea" maxlength="200"&gt;
  
  &lt;script&gt;
  
      function get_textvalue(){
  
          var ad = $("#mytextarea");
  
          var ad_words  = ad.val().split(' ').length;
          var ad_length = ad.val().length;
  
          // Contador de palabras
          $("span#words_counting").text(ad_words);
  
          if(ad_words == 20){
              $("span#words_counting").css("color", "red");
          }
  
          if(ad_length &gt;= 180){
              $("div#ad_warning").slideDown();
          }
  
      }
  
      function ad_writer(){
  
          var ad = $("#mytextarea");
  
          var ad_text   = ad.val();
          var ad_words  = ad_text.split(' ').length;
          var ad_length = ad_text.length;
  
          if(ad_words == 20){
  
              var new_ad = ad.val().substring(0,ad_length);
              $("#mytextarea").val(new_ad);
  
          }
  
          $("#avis_vendor_id").html(ad_text);
  
      }
  
  &lt;/script&gt;
  </code></pre>