views:

1515

answers:

4

I'm trying to validate the content of the HTML Editor using an ASP.net custom validator control. The idea is to check that some content has been input - the same way a required field validator works.

In the ClientValidationFunction="SomeFunction" I reference this function:

 function SomeFunction(source, args)
    {
        var editor = $find("<%=htmlEditor.ClientID%>");
        var content = editor.get_content();
        var isValid = content.length > 0;
        editor.set_content(content); 
        args.IsValid = isValid;
    }

The reason that I set the content after getting it, is that this is a hack to get the content to re-register in the editor. For some reason, if I don't reset the content on the second attempt to postback - once it's been validated - the empty content, from the first attempt, gets posted back instead of the valid content.

Does anyone know either how to check the content of the HTML Editor, without having to reset the content? Or, if it is reset using set_content(), without the font size and font style menus being de-activated?

+1  A: 

OK, solved this one by updating to the latest release (Sept 2009) of the Ajax Toolkit.

The set_content() hack is no longer necessary. Just remove this from the above javascript code and the custom validator will work. The HTML Editor now passes through the updated content to the server: "Woohoo!"

Thanks to the guys at Obout for fixing the bug! :-)

Mike
A: 

Well i have the same problem and i have the november release and still the same ... i validate i get the content but after the script is done whatever i write after is not getting posted instead i get a blank entry...

the client script is identical to yours in any way that matters ...

and to top it - really don't know why this happens - the custom validator validates on load before i even press the button that causes validation ...

Latz
+1  A: 

As I said in my previous post, you shouldn't need the set_content hack. This is my code, which I use to validate that the editor is not empty:

  <asp:CustomValidator 
                    CssClass="errorMessage" 
                    ID="HtmlEditorValidator" 
                    runat="server" 
                    ErrorMessage="Release Note cannot be empty"
                    Display="None" 
                    ControlToValidate="radEditor"
                    EnableClientScript="true"
                    ClientValidationFunction ="checkEditorNotEmpty"
                    OnServerValidate="CheckEditorNotEmptyServerSide"
                    ValidateEmptyText="true">
</asp:CustomValidator>  

function checkEditorNotEmpty(source, args)
        {
            var editor = $find("<%=radEditor.ClientID%>");
            var cont = editor.get_text();
            var isValid = cont.length > 0;
            args.IsValid = isValid;
        }

//In the code behind:

  protected void CheckEditorNotEmptyServerSide(object sender, ServerValidateEventArgs args)
        {
            bool valid = args.Value.Length > 0;
            args.IsValid = valid;
        }

This works with the September release, I'm hoping they haven't missed out the bug fix in the Novemeber release: that would be very odd.

HTH

Mike
A: 

Latz, did you ever find out why the editor validates on load? I'd really like to prevent that behavior...

Ben