Web Programming Notes

December 22, 2008

TinyMCE Character Limit Example

Filed under: Javascript — admin @ 7:59 pm

TinyMCE Character Limit Example

Below is some code illustrating how to detect a character count on a TinyMCE wysiwyg control.  Enforcing the character limit can be done in a number of ways, the most basic being an alert.  The regular expression that removes the HTML tags is optional depending on whether they are relevant in the count.

<textarea style=”width: 600px;height:300px; ”></textarea>
<script>
function characterCount() {
    var text = tinyMCE.selectedInstance.getBody().innerHTML.replace(/(<([^>]+)>)/ig,\”\”);
     if (text.length > 300)
     {
          alert(”Character limit of 300 exceeded”);
     }
}
….
tinyMCE.init({
     theme : “advanced”,
     handle_event_callback : \”characterCount\”
});
….
</script>

Powered by WordPress