Implementing TinyMCE Rich Text Editor With JS Validation

Introduction

In this article, I am going to demonstrate how to implement TinyMCE Rich text editor with the character limitation and its validation using JavaScript in your web page.

TinyMCE Rich text editor

TinyMCE Rich text editor is a text editor control. It allows formatting the text within the rich text area, such as you can set headings like <h1> to <h6>, set fonts as bold, italic, underline, and much more. You can even embed your code there in the HTML. It also provides such a great functionality like copy, paste, undo, redo, text selection within the text area etc. You also can use blocks, such as Div, pre, Paragraph and etc.

The TinyMCE Rich text editor also provides the functionality of character count and its validation. So, if you want to limit the number of allowed characters for TinyMCE editor, then you can do it using JavaScript.

Implimentation of TinyMCE Rich text editor

So, let's start the implimentation of TinyMCE Rich text editor. The following HTML code consists of TinyMCE Rich text HTML text area and simple buttons.

  1. <textarea id="txtTinyMCE" rows="2" cols="20"></textarea> <br />  
  2. <div id="character_count"> </div> <br /> <input type="submit" value="Submit" onclick="return ValidateCharacterLength();" />  
  3. <script type="text/javascript" src="//tinymce.cachefly.net/4.0/tinymce.min.js"></script>  
  4. <script type="text/javascript">  
  5.     window.onload = function() {  
  6.         tinymce.init({  
  7.             selector: 'textarea',  
  8.             width: 400,  
  9.             setup: function(ed) {  
  10.                 ed.on('keyup'function(e) {  
  11.                     var count = CountCharacters();  
  12.                     document.getElementById("character_count").innerHTML = "Characters: " + count;  
  13.                 });  
  14.             }  
  15.         });  
  16.     }  
  17.   
  18.     function CountCharacters() {  
  19.         var body = tinymce.get("txtTinyMCE").getBody();  
  20.         var content = tinymce.trim(body.innerText || body.textContent);  
  21.         return content.length;  
  22.     };  
  23.   
  24.     function ValidateCharacterLength() {  
  25.         var max = 20;  
  26.         var count = CountCharacters();  
  27.         if (count > max) {  
  28.             alert("Maximum " + max + " characters allowed.")  
  29.             return false;  
  30.         }  
  31.         return;  
  32.     }  
  33. </script>  

To count the number of characters within text area, you need to assigned KeyUp event to the TinyMCE rich text editor control as shown above. It will call the above JS functions and it will return the count of the total characters from the text area.

Now, when you click on the HTML button, the onclick property will be fired and ValidateCharacterLength() function will be called. It will return the validation of the char in textarea.

Browser compatibility of TinyMCE Rich text editor

The HTML code created above is tested in the following browsers.

Interner Exploler, Mozila Firefox, Google Chrome, Safari Web Browser, and Opera.

TinyMCEEditorOutPut

NOTE

If you are using an older version of web-browser, you should update this to the latest version.

Summary

Tiny MCE is a very useful text editor with several functionalities for our web applications.

Ebook Download
View all
Learn
View all