Home > Asp.net > FCK Editor With Custom Validation in Asp.net

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>

Advertisements
  1. TrailBlazer
    May 29, 2009 at 9:15 am

    Thanks Pankaj, you saved the day!!

  2. March 8, 2010 at 12:03 am

    beautiful shared.thanks 🙂

  3. September 28, 2011 at 12:01 pm

    very very thanks it’s very helpful me

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: