SharePoint InputFormTextBox Rich Text Validation

I had some problems validating SharePoint InputFormTextBox with InputFormRequiredFieldValidator.  The problem is that it always shows the InputFormTextBox as invalid even tho I did have some content in the text box.  And if you click the submit button the second time, the validation just doesn't work any more.

To get around it, I had to use the ASP.NET custom validator.  I found this way from other blogs online so it's nothing new.  However, there is a key attribut that a lot of blog forgot to mention.  Below is my code snippet.


function ValidateMessageBody(source, args) {
    try {
        //Create Rich TextBox Editor control object 
        var docEditor = RTE_GetEditorDocument(source.controltovalidate);
        if (null == docEditor)
            return;
        var strHtml = docEditor.body.innerText;
        if (strHtml == "") {
            args.IsValid = false;
            return;
        }
    } catch (err) { }
    args.IsValid = true;


}

<SharePoint:InputFormTextBox  runat="server" ID="iftxtText" Rows="10" RichText="true" RichTextMode="Compatible" AllowHyperlink="true" TextMode="MultiLine" />


<asp:CustomValidator cssClass="redText" ID="cvOverview" ClientValidationFunction="ValidateMessageBody" ValidateEmptyText="true" runat="server" ValidationGroup="SaveClick" ControlToValidate="iftxtText" Display="Dynamic" ErrorMessage="Required"></asp:CustomValidator>

The ValidateEmptyText attribute is required to set to true for this to work.  It's so easy and yet took me a lot of time because I wasn't aware of this.   Hopefully, it'll help others.

Comments

kraysco said…
Thanks for the post it is just what I needed!

I did have to tweak it slightly when I found that the validation for my control was passing even though it appeared empty. Further testing revealed that the textbox was holding a space character, which != "".

Modifying: if (strHtml == "") to: if (strHtml.trim() == "") did the trick.

Thanks again!

Kevin

Popular posts from this blog

SharePoint 2013 App Details Page Error

SharePoint 2013 - Working with Display Template for Content Search Web Part

SharePoint 2013 Features Information List