Validate CKEditor Using jQuery Validate

Introduction

In this article we learn about how to validate CKEditor control using jQuery validate.

Prerequisite

The following JavaScript library is used to validate CKEditor control.

Description

When we configure CKEditor control in our html page it renders in Iframe, so jquery validate library fails to check wthether this control is empty or not and we face issue while validating this control. Let’s see an example:

HTML

  1. <form>  
  2.    <textarea id="txtDemo1" name="txtDemo1"></textarea>  
  3.    <textarea id="txtDemo2" name="txtDemo2"></textarea>  
  4.    <input type="submit" value="submit">  
  5. </form>  
Let’s assume we have configured CKEditor for both text area control and both fields are required in system. For validation we have implemented following validation for both control as in the following example:

Script
  1.   $("form").validate({    ignore: [],  
  2.                   
  3.                 rules: {  
  4. txtDemo1: {  
  5.                         ckrequired: true //Custom required field   
  6.                     }  
  7. , txtDemo2: {  
  8.                         required: true  //Default required field fails  
  9.                     }  
  10.                 }});  
  11.   
  12.   
  13.   
  14.         //Extention method for check CKEditor Control   
  15. // jQuery.validator.addMethod("customfunctionanme",validationfunction,validationmessage);  
  16.   
  17.         jQuery.validator.addMethod("ckrequired", function (value, element) {  
  18.             var idname = $(element).attr('id');  
  19.             var editor = CKEDITOR.instances[idname];  
  20.             var ckValue = GetTextFromHtml(editor.getData()).replace(/<[^>]*>/gi, '').trim();  
  21.             if (ckValue.length === 0) {  
  22. //if empty or trimmed value then remove extra spacing to current control  
  23.                 $(element).val(ckValue);  
  24.             } else {  
  25. //If not empty then leave the value as it is  
  26.                 $(element).val(editor.getData());  
  27.             }  
  28.             return $(element).val().length > 0;  
  29.         }, "This field is required");  
  30.   
  31. function GetTextFromHtml(html) {  
  32.             var dv = document.createElement("DIV");  
  33.             dv.innerHTML = html;  
  34.             return dv.textContent || dv.innerText || "";  
  35.         }  
From the above code we have used both default required validation and ckrequired custom required method by extending jQuery validate library. Along with custom method we have used one JavaScript utility function to extract text from html value and after that we have trimmed value to validate trim value with extra space.

Hope this article is useful for validate CKEditor control as well as create custom validation method using jQuery validate.

Conclusion

In this article we learned about validating CKEditor using custom JQuery validation method.

 

Up Next
    Ebook Download
    View all
    Learn
    View all