API Reference: RegularExpressionAttribute Class

This attribute is part of the .NET System.ComponentModel.DataAnnotations namespace.

The RegularExpressionAttribute can be applied to string-based content model properties. The attribute sets a regular expression pattern against which the property value will be validated. The back office will display a validation error if the property value is not a match for the specified regular expression.

Usage

Decorate the model property with the RegularExpressionAttribute. Pass the regular expression pattern in the attribute constructor.

Example

public class SampleContent: IContentType
{
    // Sets a regular expression to validate a RFC 1123 hostname
    [RegularExpression("^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$")]
    public string Hostname { get; set; }

    // ...
}