Required field validator for an fckeditor

Unfortunately a requiredfieldvalidator doesn’t work with the fckeditor.

The way to do this is to use a custom validator.

 

Important note: you need to set validateemptytext=”true” otherwise it doesn’t fire

Then add the following javascript (note that I’m using jQuery here to reference the validator control)

<script type="text/javascript">// <![CDATA[
function ValidateContentText(source,args)
{
var controltovalidate = jQuery(source).attr("controltovalidate");
var fckControl = FCKeditorAPI.GetInstance(controltovalidate);
args.IsValid = fckControl.GetXHTML(true) != "";
}
// ]]></script>

You might also want to handle the server-side validation too, even though if client-side isn’t working then the fckeditor won’t be working…

Protected Sub cvIntro_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles cvIntro.ServerValidate

Dim myContentPlaceHolder As ContentPlaceHolder

myContentPlaceHolder = CType(Master.FindControl("cphMain"), ContentPlaceHolder)

Dim fckEditorToCheck As FredCK.FCKeditorV2.FCKeditor

fckEditorToCheck = myContentPlaceHolder.FindControl(CType(source, CustomValidator).ControlToValidate)

args.IsValid = Not String.IsNullOrEmpty(fckEditorToCheck.Value)

End Sub

Update 2011

var controltovalidate = jQuery(source).attr("controltovalidate");

No longer works... just use

var controltovalidate = source.controltovalidate;

Instead - easy peasy

This entry was posted by nerdboy on Tuesday, October 7th, 2008 at 3:39 pm and is filed under asp.net. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response below, or trackback from your own site.

Show/Hide Comments (2)

  • http://www.ixtentia.com/application-testing.html iPhone application testing

    Thanks for sharing code and simple explanation…

  • roy

    thanks for sharing