Word Count Validator
Enforce content length. This script tracks the number of words typed in a textarea, a common requirement for academic or editorial submissions.
0 words
Limit: 10
Copy the Script
<script>
function countWords(val) {
// Split by whitespace and filter empty entries
var words = val.trim().split(/\s+/);
var count = words.length;
if(val.trim() === "") count = 0;
document.getElementById("display").innerHTML = count + " words";
if(count > 100) {
alert("Word limit exceeded!");
}
}
</script>
<textarea oninput="countWords(this.value)"></textarea>
<div id="display">0 words</div>
Frequently Asked Questions
It typically splits the string by whitespace using Regex `/\s+/`. It then filters out empty strings to avoid counting spaces as words.
Yes. You can disable the input or show an error message if `wordCount > maxLimit`.
Yes. Using the `oninput` event ensures the count updates immediately, regardless of how the text entered the field.