Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count Words and Characters [closed]

I've been looking to add a word and character count to a textarea using jQuery although all I've found are plugins to limit characters and words.

I would like to update in real-time, so that every time a user adds a character, or a word, the counter updates.

Is there a simple way to check for words and characters?

like image 467
user1919937 Avatar asked Dec 23 '12 11:12

user1919937


People also ask

How do you check word count without punctuation?

How to use an online word counter and character counter tool. 123WordCounter is incredibly easy to use and can give you realtime results. Just type or paste your text in the text box, and it will show you the word count for the number of words, characters, sentences, and paragraphs.

How do I get a character count in word?

You can get a character count in a Word document by selecting the "Review" tab and clicking "Word Count." You can find both the number of characters with spaces and the character count not including spaces. You can add the Word Count dialog box to the Quick Access toolbar so it's always one click away.

Do you count characters with or without spaces?

Yes, the character count includes all spaces, punctuation and letters. Anything that moves your cursor counts as a character.

How many words is 3000 characters with spaces?

Answer: 3,000 characters is between 428 words and 750 words with spaces included in the character count.


2 Answers

function wordCount(val) {
  var wom = val.match(/\S+/g);
  return {
    charactersNoSpaces: val.replace(/\s+/g, '').length,
    characters: val.length,
    words: wom ? wom.length : 0,
    lines: val.split(/\r*\n/).length
  };
}


var textarea = document.getElementById('text');
var result = document.getElementById('result');

textarea.addEventListener('input', function() {
  var wc = wordCount(this.value);
  result.innerHTML = (`
   <br>Characters (no spaces):  ${wc.charactersNoSpaces}
   <br>Characters (and spaces): ${wc.characters}
   <br>Words: ${wc.words}
   <br>Lines: ${wc.lines}
  `);
});
<textarea id="text" cols="30" rows="4"></textarea>
<div id="result"></div>
like image 135
Roko C. Buljan Avatar answered Oct 23 '22 19:10

Roko C. Buljan


char_count = $("#myTextArea").val().length;
word_count = $("#myTextArea").val().split(" ").length;
like image 37
ATOzTOA Avatar answered Oct 23 '22 19:10

ATOzTOA