Posts Tagged ‘fckeditor’

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.

 <asp:customvalidator runat="server" validateemptytext="true" id="cvIntro" setfocusonerror="true" display="none"
                errormessage="Please enter an introduction" clientvalidationfunction="ValidateContentText"
                 controltovalidate="fckIntro"></asp:customvalidator>

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">
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