jQuery: Validate FCKEditor using jQuery

07 Mar 2009

I’ve written a quick jQuery plugin which allows you to validate an FCKEditor instancing using jQuery.  This plugin will both validate FCKEditor instances both on-the-fly (as you type), and when you attempt to submit a form.

1. Download the plugin

2. Implement the plugin:

  1. $(document).ready(function() {
  2. if(typeof jQuery.fn.fckEditorValidate == ‘function’) {
  3. jQuery.fn.fckEditorValidate({instanceName: ‘content’});
  4. }
  5. });

The full list of arguments which may appear in the brackets are:

  1. instanceName: The name of the FCKEditor instance (default: ‘content’),
  2. preventFormSubmit: Prevent form from submitting if FCKEditor instance is empty (default: true),
  3. showValidationError: Show a message if the FCKEditor instance is empty (default: true),
  4. showValidationErrorBeforeFCKEditor: Show the message before the FCKEditor on the page (default: false – i.e. shows message below FCKEditor),
  5. validationErrorClass: CSS class to attribute to label showing validation error message (default: ‘fckValidateError’),
  6. validationErrorMessage: Message to show when FCKEditor instance is empty (default: ‘Please complete this field’)

This is by no means an exhaustive or complete solution; you should always implement some server-side validation. Javascript cannot be solely relied upon, as users may not have it in their web browser, or may choose to disable it. There’s also some known issues with IE with the on-the-fly (as you type) validation being a bit hit and miss.

When the form is submitted, this plugin will kick in and has been tested on IE6, 7, Firefox 3 and Safari 3.

Comment #1

17 Mar 2009
  • Posted at 15:01:25
  • Rey Muradaz

You need to replace the single quotes in your code sample above–the ones there are smart quotes, so a straight cut and paste doesn’t work. I have managed to get the plugin to ‘work’ in that it won’t let me submit an empty fckeditor instance, but it’s not showing any error message. Any ideas?

Comment #2

19 Mar 2009
  • Posted at 05:40:25
  • admin

Hi Rey,

Thanks for the heads up – I’ll take a look at the quote issue.

Aside from explicitly stating the arguments I’ve listed above, I’m not sure why the error message won’t show. It might be worth viewing your generated page source using Firebug or similar in Firefox when you attempt to submit the blank FCKEditor, and see what HTML markup appears (if any).

Comment #3

22 Jun 2009
  • Posted at 07:43:25
  • Michael Stone

I’m unable to get this plugin working. Any ideas?

I’m using this code:

if(typeof jQuery.fn.fckEditorValidate == ‘function’) {
jQuery.fn.fckEditorValidate({instanceName: ‘description’,
preventFormSubmit: ‘true’,
showValidationError: ‘true’,
validationErrorMessage: ‘Please provide a description.’
});
}

Comment #4

24 Jul 2009
  • Posted at 02:22:00
  • wo_is神仙

Well done

Comment #5

24 Jul 2009
  • Posted at 02:26:27
  • wo_is神仙

I’m using this code:

var oEditor = FCKeditorAPI.GetInstance(‘Content’);
oEditor.GetXHTML(true);

Comment #6

14 Dec 2009
  • Posted at 03:05:34
  • vincent

Great Job!

Comment #7

12 Feb 2010
  • Posted at 04:26:47
  • Praveen Kumar

Hi Tim,
I have used the Plug to my site bit it not validating the FCKeditor, I need to submit the form twice then only the form is submitted.
Can you please help me.

Thanks in Advance

Post Comment