Search This Blog

Wednesday, April 6, 2011

Adding and Validating SharePoint InputFormTextBox Control

Recently, I have faced an issue while validating "InputFormTextBox / Rich Text Box" client side.

"InputFormTextBox" is an sharepoint control. To use this control in your custom page following steps must be followed:

1. Add the required directive at the top of the ascx page (if you're using a web user control):
<%@ Register TagPrefix="SharePoint Namespace="Microsoft.SharePoint.WebControls"Assembly="Microsoft.SharePoint,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>

2. For creating instance of SharePoint Rich Text Box (InputFormTextBox) control in your ascx (if you're using a web user control):

<SharePoint:InputFormTextBox ValidationGroup="UserRegistrationGroup" runat="server" ID="MessageBody" Rows="15" Columns="40" RichText="true" RichTextMode="FullHtml" AllowHyperlink="true" TextMode="MultiLine" CausesValidation="true" />


To validate this control:

3. Add Custom Validator



<asp:CustomValidator CssClass="requirefieldtxt" ID="CustomValidator1" ClientValidationFunction="ValidateMessageBody" runat="server" ValidationGroup="UserRegistrationGroup" ControlToValidate="MessageBody" Display="Dynamic" ErrorMessage="Please input valid message."></asp:CustomValidator>


Custom Validator Code:

<script language="javascript" type="text/javascript">
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;


}


</script>

This will resolve the validation issue.



4 comments:

  1. I tried using the above script in custom ASPX page(deployed in layouts), but scritp is not working.
    Do I need to pass any arguments in "ValidateMessageBody" fucntion, if ys kindly suggest how I will pass values.

    ReplyDelete
  2. Hi,

    I have tried this in Custom ASPX page (deployed in layouts), and is working fine for me.

    Check one thing that your script tag is well formed.

    Also, If you are trying this validation on button click then Validation group of all the controls (validations also) must be the same.

    Please try these at your end and let me know if you still find some issues.

    Thanks,
    Parveen Siwach

    ReplyDelete
  3. Code was truncated by the editor. We've updated the code. Please try with new code and let us know if you still face the same error.

    You donot require to pass any parameter in the ValidateMessageBody function because this is handled internally by CustomValidator

    ReplyDelete
  4. Fantastic, I've been trying various solutions all-day for SharePoint 2010, this did the trick! Cheers!!!

    ReplyDelete