/ Published in: JavaScript
Javascript code for counting the number of words in a sentence. It can be used to allow only a certain number of words.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function WordCount() { var wordsallowed = 300; var inp = document.getElementById('txtabstract').value; var regex = /\s+/gi; var wordcount = jQuery.trim(inp).replace(regex, ' ').split(' ').length; if (wordcount <= wordsallowed) { chars = inp.length; } else { var text = inp.val(); var new_text = text.substr(0, chars); document.getElementById('txtabstract').value=new_text; wordcount--; } $("#labelcount").html(wordcount); }