Welcome to the navigation

Dolor ut tempor eiusmod adipisicing et minim qui ut culpa incididunt dolore non exercitation occaecat pariatur, enim sunt excepteur ut sint ipsum consectetur elit, consequat. Ut qui in consequat, laborum, ut id veniam, est cupidatat magna labore sint enim nisi dolore elit, eiusmod in in voluptate aliqua, nostrud nulla laboris

Yeah, this will be replaced... But please enjoy the search!

Validate Optimizely XhtmlString property length

Since the built in attributes [StringLength], [MinLength] and [MaxLength] doesn't work when using a XhtmlString property we need to implement a custom ValidationAttribute.

The goal

The goal is to have an attribute that only will count the characters entered excluding any HTML. We must be able to set the min length and the max length, also it would be nice to have the possibility to enter a custom error message

The list of demands are

  • Must validate written text excluding any HTML-markup
  • Must have a MinLength
  • Must have a MaxLength
  • Should have an option to set a custom error message
[Display(Name = "MainContent")]
[XhtmlStringLengthValidation(50, 500)]
public virtual XhtmlString MainContent{ get; set; }

Custom error message example

[XhtmlStringLengthValidation(50, 500, CustomErrorMessage = "The Main content field must contain more than {0} characters but less than {1}.")]


A ValidationAttribute example

using EPiServer.Core;
using EPiServer.Core.Html;
using System.ComponentModel.DataAnnotations;
using System.Web;

public class XhtmlStringLengthValidationAttribute : ValidationAttribute
    public string? CustomErrorMessage { get; set; }

    private readonly int _minLength;
    private readonly int _maxLength;

    public XhtmlStringLengthValidationAttribute(int minLength, int maxLength)
        _minLength = minLength;
        _maxLength = maxLength;

    public override bool IsValid(object value)
        if (_minLength == 0 && value is null)
            return true;

        var propertyDecodedText = GetDecodedText(value);

        return !(propertyDecodedText.Length < _minLength || propertyDecodedText.Length > _maxLength);

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        var result = base.IsValid(value, validationContext);

        if (!string.IsNullOrWhiteSpace(result?.ErrorMessage))
            var propertyDecodedText = GetDecodedText(value);

            result.ErrorMessage = string.IsNullOrEmpty(CustomErrorMessage)
                ? $"{result.ErrorMessage} The text must be between {_minLength} and {_maxLength} long ({propertyDecodedText.Length})."
                : string.Format(CustomErrorMessage, _minLength, _maxLength);

        return result;

    private string GetDecodedText(object value)
        if (value is XhtmlString property)
            var propertyHtml = property.ToString();
            var propertyPlainText = TextIndexer.StripHtml(propertyHtml, maxTextLengthToReturn: propertyHtml.Length);
            var propertyDecodedText = HttpUtility.HtmlDecode(propertyPlainText);

            return propertyDecodedText;

        return string.Empty;


A similar result can be achieved using IValidate, I do hoever prefer using attributes since they are more targeted and implementation specific.