Friday, May 15, 2009

FCK Editor With Custom Validation in Asp.net

·

I have to keep validation for fckeditor in my application, but then i came to know that Requiredfield Validator control of asp.net does not work with fckeditor. So I used CustomValidator control of asp.net and it worked perfectly.

You might be aware that customvalidator works in both ways. either clientsidevalidation function using javascript and servervalidate. Difference between them is servervalidate method takes server trip while in clientsidevalidation all validations fire at once without server trip.

I will be showing you both ways:

1) Using ClientValidationFunction

<script language="javascript" type="text/javascript">
function ValidateFCK(source, args) {


var fckBody = FCKeditorAPI.GetInstance('<%=FCKeditor1.ClientID %>');
args.IsValid = fckBody.GetXHTML(true) != "";
}
</script>


<FCKeditorV2:FCKeditor ID="FCKeditor1" BasePath="../fckeditor/" runat="server">
</FCKeditorV2:FCKeditor>


<asp:CustomValidator runat="server" ErrorMessage="Please Enter Description" ID="CustValDesc" ControlToValidate="FCKeditor1"
ClientValidationFunction="ValidateFCK" ValidateEmptyText="True"></asp:CustomValidator>

Note: Sometimes this method does not works with multiple browsers.

2) Using OnServerValidate
<FCKeditorV2:FCKeditor ID="FCKeditor1" BasePath="../fckeditor/" runat="server">
</FCKeditorV2:FCKeditor>


<asp:CustomValidator runat="server" ErrorMessage="Please Enter Description" ID="CustValDesc" ControlToValidate="FCKeditor1"
ValidateEmptyText="True"
onservervalidate="CustValDesc_ServerValidate"></asp:CustomValidator>

protected void CustValDesc_ServerValidate(object source, ServerValidateEventArgs args)
{
if (FCKeditor1.Value == string.Empty )
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}

Note:

Whenever you are submitting form and you have kept validations, always check whether the page is valid or not by:
if (Page.IsValid)
{


//your code


}

I also found some other method which works for Requiredfield validator with fckeditor is
EnableClientScript="false" and

InitialValue="<br/>"

<asp:RequiredFieldValidator ID="req_Description" runat="server" ControlToValidate="FCKeditor1"
Display="Dynamic" InitialValue="<br/>" Text="Please Enter Description"
EnableClientScript="false"></asp:RequiredFieldValidator>

0 comments: