Posted By

techval on 12/26/17


Tagged

count word


Versions (?)

JavaScript Word Count


 / Published in: JavaScript
 

URL: http://www.classgist.com

Javascript code for counting the number of words in a sentence. It can be used to allow only a certain number of words.

  1. function WordCount() {
  2. var wordsallowed = 300;
  3. var inp = document.getElementById('txtabstract').value;
  4.  
  5. var regex = /\s+/gi;
  6. var wordcount = jQuery.trim(inp).replace(regex, ' ').split(' ').length;
  7. if (wordcount <= wordsallowed) {
  8. chars = inp.length;
  9. } else {
  10. var text = inp.val();
  11. var new_text = text.substr(0, chars);
  12. document.getElementById('txtabstract').value=new_text;
  13. wordcount--;
  14. }
  15. $("#labelcount").html(wordcount);
  16. }

Report this snippet  

You need to login to post a comment.