Search This Blog

Thursday, March 19, 2015

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="SharePointNamespace="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.


No comments:

Post a Comment