RegularExpressionAttribute.Pattern Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets the regular expression to use for validation of the associated member.

Namespace:  System.ComponentModel.DataAnnotations
Assembly:  System.ComponentModel.DataAnnotations (in System.ComponentModel.DataAnnotations.dll)

Syntax

'Declaration
Public Property Pattern As String
public string Pattern { get; private set; }

Property Value

Type: System.String
The regular expression to use for validation of the associated member.

Remarks

The value of the property is not tested against the pattern when the property contains nulla null reference (Nothing in Visual Basic) or an empty string ("").To validate that the value is not nulla null reference (Nothing in Visual Basic) or an empty string, use the RequiredAttribute attribute.

Examples

The following examples show how to apply the RegularExpressionAttribute attribute to a property that contains values for a last name.

Public Class Customer
    <Required()> _
    <RegularExpression("^[a-zA-Z''-'\s]{1,40}$", _
          ErrorMessage:="Numbers and special characters are not allowed in the last name.")> _
    Public Property LastName() As String
        'Implement Get and Set logic
    End Property
End Class
public class Customer
{
  [Required]
  [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$", 
      ErrorMessage="Numbers and special characters are not allowed in the last name.")]
  public string LastName { get; set; }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.