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
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.
2 Reader Comments (Reply Now)
November 10th, 2009
@ 8:40 am
Thanks for sharing code and simple explanation…
July 23rd, 2010
@ 8:25 am
thanks for sharing
Leave a Reply