Word Count and Word Limit
editor.getWordCount()andeditor.getCharCount()count the visible text, not the markup.- Set
config.maxWordsto cap the length. Once the limit is reached, typing a new word is blocked and the counter in the corner of the editor turns red. - A paste or
setHTMLCode()can still go over, so checkeditor.isOverLimit()before you accept the form. Try the "Load 70 words" button. - For a character limit instead, use
config.maxLength; see the max length demo.
Loading the editor...
<div id="div_editor1"></div>
<p id="counts"></p>
<script>
var config = {};
config.maxWords = 50; // 0 or unset = no limit
config.charLimitShowCounter = true; // the floating counter in the corner
var editor1 = new RichTextEditor("#div_editor1", config);
function showCounts() {
document.getElementById("counts").textContent =
editor1.getWordCount() + " words, " + editor1.getCharCount() + " characters";
}
editor1.attachEvent("change", showCounts);
showCounts(); // setHTMLCode() does not fire "change": call it after loading too
// Typing a new word past the limit is blocked. A paste can still go over,
// so check again before you accept the form.
function canSubmit() {
return !editor1.isOverLimit();
}
</script>