i'm using the fckeditor as the rich text box control. The problem is, it cannot validate the value correctly. For example, When i've inserted a text in the rich text box control, i click the submit button. Then, the requiredfieldvalidator indicates that the
field is still empty. How to fix this problem?
Taj Freewarez
No limitations...It is not a shareware...It is simply free :)
http://tajfreeware.blogspot.com
"FCKeditor will not work properly with the Required Field Validator when the "EnableClientScript" property of the validator is set to "true" (default). Due to a limitation in the default validation system, you must set it to "false".
If you want to do client side validation, you must use a Custom Validator instead and provide the appropriate validation function, using the FCKeditor JavaScript API."
So the simple solution is: Disable client side validation for the control. (IE you add
EnableClientScript="False" to the validator properties).
Regarding how to hook up a client side custom validator to the FCKEditor JavaScript API: Can anyone offer help on how to do this?
<asp:CustomValidator runat="server" ID="cvBody" SetFocusOnError="true" Display="dynamic" Text="The Body is required" ClientValidationFunction="ValidateContentText"></asp:CustomValidator>
Add this Javascript function to your page
function ValidateContentText(source,args)
{
var fckBody= FCKeditorAPI.GetInstance('<%=FCKeditor1.ClientID %>');
args.IsValid = fckBody.GetXHTML(true) != "";
}
Here's some (rather verbose) code I use to do server side validation. The example is for an update - when the input is invalid the details view re-opens with all the form fields populated with whatever was there when the form was submitted. On an insert
it just opens an empty form - inconvenient for the user but I can't think of a workaround.
string descriptionShort = ((FCKeditor)FooView.FindControl("CkeDescriptionShort")).Value;
string descriptionLong = (((FCKeditor)FooView.FindControl("CkeDescriptionLong")).Value).Replace("<br
/>", "");
string active = ((CheckBox)FooView.FindControl("ActiveCheckBox")).Checked.ToString();
// Validate the editor input. Minimums are set = 7 so as to consider the <p></p> tags
int descriptionShortLength = ((FCKeditor)FooView.FindControl("CkeDescriptionShort")).Value.Length;
int descriptionLongLength = ((FCKeditor)FooView.FindControl("CkeDescriptionLong")).Value.Length;
int descriptionShortLengthMin = 7;
int descriptionShortLengthMax = 100;
int descriptionLongLengthMin = 7;
int descriptionLongLengthMax = 500;
if (descriptionShortLength < descriptionShortLengthMin)
{
ServerSideValidationLabel.Text = "You must provide a short description.";
FooView.ChangeMode(DetailsViewMode.Edit);
FooView.Visible = true;
}
else
if (descriptionShortLength > descriptionShortLengthMax)
{
ServerSideValidationLabel.Text = "Your description is too long, please edit it by removing " +(descriptionShortLength - descriptionShortLengthMax)
+ " characters from your input.";
FooView.ChangeMode(DetailsViewMode.Edit);
FooView.Visible = true;
}
else
if (descriptionLongLength < descriptionLongLengthMin)
{
ServerSideValidationLabel.Text = "You must provide a long description.";
FooView.ChangeMode(DetailsViewMode.Edit);
FooView.Visible = true;
}
else
if (descriptionLongLength > descriptionLongLengthMax)
{
ServerSideValidationLabel.Text =
"Your description is too long, please edit it by removing " +
(descriptionLongLength - descriptionLongLengthMax) + " characters from your input.";
FooView.ChangeMode(DetailsViewMode.Edit);
FooView.Visible = true;
}
else
{
//If the editor input is valid pass validation process to Page validation
if (Page.IsValid)
{
// Pass your parameters to the DAL or whatever
}
else
{
ServerSideValidationLabel.Text = "There was a problem updating the form.<br />Please check for " +
"missing values or incorrect formats.<br />";
FooView.Visible = true;
I was having the same problem and got very easy solution. Change RequiredFieldValidator's properties EnableClientScript=False and InitialValue=<br />. RequiredFieldValidator works fine.
Unfortunately none of above solutions worked for me. When I click Save button on my page, the code for the button executes and data validity control code handles empty FCKEditor. Customvalidator does not seem to do anything. I get no client side errors but
no reaction as well. I think setting EnableClientScript to false makes my button code execute before customvalidator mechanism. I'll try to overcome that problem and let you know if I find another solution.
Ok. What worked in client side for me (although I couldn't manage to make AJAX.NET validator callout extender to display its message properly and had to cancel it and were forced to use classic required field validator instead) is as below:
Member
1 Points
38 Posts
cannot validate field in fckeditor
Aug 28, 2006 02:34 AM|aristotle85|LINK
No limitations...It is not a shareware...It is simply free :)
http://tajfreeware.blogspot.com
None
0 Points
4 Posts
Re: cannot validate field in fckeditor
Sep 02, 2006 08:14 AM|yoshinanda|LINK
hi,
i am in the same boat. Please let me knw if you have solved the issue.
Please let me know as soon a possible as the site is "UP n Live" n i am not able to fix the validation of fckeditor.
Kind Regards
Yoshita Nanda
None
0 Points
5 Posts
Re: cannot validate field in fckeditor
Aug 06, 2007 06:56 AM|mpowrie|LINK
According to the FCKEditor wiki:
"FCKeditor will not work properly with the Required Field Validator when the "EnableClientScript" property of the validator is set to "true" (default). Due to a limitation in the default validation system, you must set it to "false".
If you want to do client side validation, you must use a Custom Validator instead and provide the appropriate validation function, using the FCKeditor JavaScript API."
So the simple solution is: Disable client side validation for the control. (IE you add EnableClientScript="False" to the validator properties).
Regarding how to hook up a client side custom validator to the FCKEditor JavaScript API: Can anyone offer help on how to do this?
None
0 Points
2 Posts
Re: cannot validate field in fckeditor
Aug 28, 2007 02:33 AM|deusBlue|LINK
Participant
1221 Points
544 Posts
Re: cannot validate field in fckeditor
Sep 10, 2007 05:27 AM|ShailAtlas|LINK
Hello,
I have used the FCkEditor javascript API to validate input. I am using Customvalidator.
All goes well on first time. But when page is posted back and I make the all empty. validator do not get fired.
Why so ?
Member
58 Points
90 Posts
Re: cannot validate field in fckeditor
Mar 14, 2008 06:30 AM|AzharSaiyed|LINK
Imamshah Bava
9898404049
Contributor
2055 Points
648 Posts
Re: cannot validate field in fckeditor
Mar 14, 2008 06:40 AM|virendra1983|LINK
Hi,
Declare a Custom Validator like this..
<asp:CustomValidator runat="server" ID="cvBody" SetFocusOnError="true" Display="dynamic" Text="The Body is required" ClientValidationFunction="ValidateContentText"></asp:CustomValidator>
Add this Javascript function to your page
function ValidateContentText(source,args)
{
var fckBody= FCKeditorAPI.GetInstance('<%=FCKeditor1.ClientID %>');
args.IsValid = fckBody.GetXHTML(true) != "";
}
Member
15 Points
57 Posts
Re: cannot validate field in fckeditor
May 06, 2008 03:48 PM|Nola3Nut|LINK
I modified what virendra1983 had just a bit. I was getting an due to having <%=FCKeditor1.ClientID %> within the javascript.
<script type="text/javascript">
function ValidateContentText(source,args)
{
var fckinEditorClientID = document.getElementById('hdfFCKClientID');
var fckBody= FCKeditorAPI.GetInstance(fckinEditorClientID.value);
args.IsValid = fckBody.GetXHTML(true) != "";
}
</script>
<FCKeditorV2:FCKeditor ID="fckContent" runat="server"></FCKeditorV2:FCKeditor>
<asp:CustomValidator runat="server" ID="cvContent" SetFocusOnError="true" ValidationGroup="Insert" Display="dynamic" Text="Content is required" ClientValidationFunction="ValidateContentText"></asp:CustomValidator>
<asp:HiddenField ID="hdfFCKClientID" runat="server" />
codebehind page:
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { hdfFCKClientID.Value = fckContent.ClientID; } }Member
2 Points
55 Posts
Re: cannot validate field in fckeditor
Nov 19, 2008 06:12 AM|aspnetAficionado|LINK
Hi
This works perfectly. However when I apply a master page (.net 2.0) to this, the validation is not fired.
Any ideas????
Member
2 Points
55 Posts
Re: cannot validate field in fckeditor
Nov 19, 2008 09:33 AM|aspnetAficionado|LINK
Hi
Just found the way for this. I changed the line above to this one:
var fckinEditorClientID = document.getElementById('ctl00_ContentPlaceHolder1_hdfFCKClientID');
The thing is all control names used in a master page change in the browser output (see source page) . So hidden control'hdfFCKClientID' becomes 'ctl00_ContentPlaceHolder1_hdfFCKClientID'.
I dont think it's very elegant and I'm sure there will be better ways to do it. But this works for me so far!
Member
93 Points
319 Posts
Re: cannot validate field in fckeditor
Nov 21, 2008 04:49 PM|GDB|LINK
Here's some (rather verbose) code I use to do server side validation. The example is for an update - when the input is invalid the details view re-opens with all the form fields populated with whatever was there when the form was submitted. On an insert it just opens an empty form - inconvenient for the user but I can't think of a workaround.
string descriptionShort = ((FCKeditor)FooView.FindControl("CkeDescriptionShort")).Value; string descriptionLong = (((FCKeditor)FooView.FindControl("CkeDescriptionLong")).Value).Replace("<br />", ""); string active = ((CheckBox)FooView.FindControl("ActiveCheckBox")).Checked.ToString(); // Validate the editor input. Minimums are set = 7 so as to consider the <p></p> tags int descriptionShortLength = ((FCKeditor)FooView.FindControl("CkeDescriptionShort")).Value.Length; int descriptionLongLength = ((FCKeditor)FooView.FindControl("CkeDescriptionLong")).Value.Length; int descriptionShortLengthMin = 7; int descriptionShortLengthMax = 100; int descriptionLongLengthMin = 7; int descriptionLongLengthMax = 500; if (descriptionShortLength < descriptionShortLengthMin){
ServerSideValidationLabel.Text = "You must provide a short description.";FooView.ChangeMode(DetailsViewMode.Edit);
FooView.Visible = true;}
else if (descriptionShortLength > descriptionShortLengthMax){
ServerSideValidationLabel.Text = "Your description is too long, please edit it by removing " +(descriptionShortLength - descriptionShortLengthMax) + " characters from your input.";FooView.ChangeMode(DetailsViewMode.Edit);
FooView.Visible = true;}
else if (descriptionLongLength < descriptionLongLengthMin){
ServerSideValidationLabel.Text = "You must provide a long description.";FooView.ChangeMode(DetailsViewMode.Edit);
FooView.Visible = true;}
else if (descriptionLongLength > descriptionLongLengthMax){
ServerSideValidationLabel.Text =
"Your description is too long, please edit it by removing " + (descriptionLongLength - descriptionLongLengthMax) + " characters from your input.";FooView.ChangeMode(DetailsViewMode.Edit);
FooView.Visible = true;}
else{
//If the editor input is valid pass validation process to Page validation if (Page.IsValid){
// Pass your parameters to the DAL or whatever}
else{
ServerSideValidationLabel.Text = "There was a problem updating the form.<br />Please check for " + "missing values or incorrect formats.<br />"; FooView.Visible = true;}
}
None
0 Points
6 Posts
Re: cannot validate field in fckeditor
Jan 06, 2009 08:12 AM|nyalati|LINK
Hey virendra,
Thank you very much . I got succeeded with your code.
Member
234 Points
106 Posts
Re: cannot validate field in fckeditor
Jan 27, 2009 02:13 AM|amoljk2009|LINK
Hi,
I was having the same problem and got very easy solution. Change RequiredFieldValidator's properties EnableClientScript=False and InitialValue=<br />. RequiredFieldValidator works fine.
None
0 Points
9 Posts
Re: cannot validate field in fckeditor
Feb 23, 2010 05:56 AM|ymeric|LINK
Unfortunately none of above solutions worked for me. When I click Save button on my page, the code for the button executes and data validity control code handles empty FCKEditor. Customvalidator does not seem to do anything. I get no client side errors but no reaction as well. I think setting EnableClientScript to false makes my button code execute before customvalidator mechanism. I'll try to overcome that problem and let you know if I find another solution.
www.onspeedtr.com
www.sagligimizicin.com
www.hukukyazilim.com
None
0 Points
2 Posts
Re: cannot validate field in fckeditor
Feb 23, 2010 07:01 AM|TodPalin|LINK
Slight modification of the code above, a "cleaner" way of finding the FCKEditor id:
Where cvBody is the .net name of your CustomValidator and BodyEditor1 is the .net name of your FCKEditor.
Also your validator will need to be nested in the same control/area as the FCKEditor for the regex to properly convert one to the other.
FCKeditor FcKeditor. Javascript Regular Expression Regex Custom Validators
None
0 Points
9 Posts
Re: cannot validate field in fckeditor
Feb 23, 2010 11:31 AM|ymeric|LINK
Ok. What worked in client side for me (although I couldn't manage to make AJAX.NET validator callout extender to display its message properly and had to cancel it and were forced to use classic required field validator instead) is as below:
<asp:CustomValidator ID="cvKampanya" runat="server" ClientValidationFunction="ValidateFCK" ControlToValidate="mKampanya" Display="Dynamic" ErrorMessage="Enter message" Font-Bold="True" Font-Size="Small" SetFocusOnError="True" ValidateEmptyText="True"></asp:CustomValidator>
<FCKeditorV2:FCKeditor ID="mKampanya" runat="server" BasePath="/Bodrum-Guide/MetinEditoru/" Height="500px" Width="100%" ToolbarSet="Bodrum"></FCKeditorV2:FCKeditor>
<script language="javascript" type="text/javascript">
function ValidateFCK(source, args) {
var fckBody = FCKeditorAPI.GetInstance('<%=mKampanya.ClientID %>');
args.IsValid = fckBody.GetXHTML(true) != "";
}
</script>
www.onspeedtr.com
www.sagligimizicin.com
www.hukukyazilim.com