RegularExpressionAttribute.Pattern Property

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)

public string Pattern { get; private set; }

Property Value

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

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

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 string LastName { get; set; }
}


Silverlight

Supported in: 5, 4, 3

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

Community Additions

ADD
Show: