Restrict HTML Site Column
-
29-09-2020 - |
Question
Is it possible to restrict a Site Column in following ways:
- Limit HTML to just use following tags:
<p>, <a>, <ul>, <ol>, <b>, <i> (or <strong> and <em>)
- and (not that important but also needed) to limit HTML Site Column on MaxLength
Is there anyway I can acomplish this?
Solution 2
we ended up by not restricting the editor, but the Search display templates. XSLT-Code:
<!-- remove html tags -->
<!-- source: http://dotnetslackers.com/Community/blogs/kaushalparik/archive/2008/08/30/strip-out-html-tags-to-display-plain-text-in-xslt.aspx -->
<xsl:template name="removeHtmlTags">
<xsl:param name="html"/>
<xsl:choose>
<xsl:when test="contains($html, '<')">
<xsl:value-of select="substring-before($html, '<')"/>
<!-- Recurse through HTML -->
<xsl:call-template name="removeHtmlTags">
<xsl:with-param name="html" select="substring-after($html, '>')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$html"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
OTHER TIPS
as far as I know, site columns are wysiwyg (what you see is what you get).
you have to choose whatever type you need from a limited list of types, and apply the avaible customization options.
the easiest way to avoid problems is to set the column as single line of text (in which case you could set the maxlength upfront or use a formula in the list validation settings).
=IF(LEN(Title)<5,TRUE,FALSE)
however, if you really want html, depending on your context, you can use the usual validators from the asp.net framework
<asp:RegularExpressionValidator ID="RegExp1" runat="server"
ErrorMessage="input allowed cannot exceed 500 characters"
ControlToValidate=" myControl"
ValidationExpression="^(?s)(.){0,500}$" />
you may need to register them as safe
or others provided by sharepoint
InputFormRequiredFieldValidator
InputFormRangeValidator
InputFormCompareValidator
InputFormRegularExpressionValidator
InputFormCheckBoxListValidator
InputFormCustomValidator
http://karinebosch.wordpress.com/sharepoint-controls/sharepoint-validation-controls/
otherwise you can always validate on the client side
$('textarea').each(function() {
if ($(this).val().match(/<(\w+)((?:\s+\w+(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)>/)) {
alert('html found');
}
});
https://stackoverflow.com/questions/5422505/jquery-validation-to-check-text-contains-no-html
and considering you want both maxlength and one (or more) expressions/validations, you may want to consider a custom validator
server-side
<sharepoint:InputFormTextBox ID="UsernameTextBox" runat="server" class="ms-input"
Title="Username" TextMode="SingleLine" Columns="60" />
<sharepoint:InputFormCustomValidator ID="UsernameCustomValidator" runat="server" Display="Dynamic" SetFocusOnError="true"
ControlToValidate="UsernameTextBox"
OnServerValidate="ValidateUserName"
ErrorMessage="Your user name must be at least 10 characters long (server-side validation)."
ValidateEmptyText="true" />
//code behind
protected InputFormTextBox UsernameTextBox;
protected InputFormCustomValidator UsernameCustomValidator;
protected void ValidateUserName(object source, ServerValidateEventArgs args)
{
if (args.Value.Length >= 10)
args.IsValid = true;
else
args.IsValid = false;
}
client-side
<sharepoint:InputFormTextBox ID="UsernameTextBox" runat="server" class="ms-input"
Title="Username" TextMode="SingleLine" Columns="60" />
<sharepoint:InputFormCustomValidator ID="UsernameCustomValidator" runat="server" Display="Dynamic" SetFocusOnError="true"
ControlToValidate="UsernameTextBox"
ClientValidationFunction="IsValidUsername"
ErrorMessage="Your user name must be at least 10 characters long (client-side validation)."
ValidateEmptyText="true" />
//page placeholder
<asp:Content ID="Main" runat="server" ContentPlaceHolderID="PlaceHolderMain">
<script language="javascript">
function IsValidUsername(source, args)
{
if (args.Value.length >= 10)
args.IsValid = true;
else
args.IsValid = false;
}
</script>
<!- - rest of the controls-->
</asp:Content>