Export (0) Print
Expand All

RegularExpressionAttribute Class

Designates a regular expression to use for validation of the associated member.

System.Object
  System.Attribute
    System.ComponentModel.DataAnnotations.ValidationAttribute
      System.ComponentModel.DataAnnotations.RegularExpressionAttribute

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

'Declaration
<AttributeUsageAttribute(AttributeTargets.Property Or AttributeTargets.Field Or AttributeTargets.Parameter, AllowMultiple := False)> _
Public Class RegularExpressionAttribute _
	Inherits ValidationAttribute

The RegularExpressionAttribute type exposes the following members.

  NameDescription
Public methodRegularExpressionAttributeInitializes a new instance of the RegularExpressionAttribute class.
Top

  NameDescription
Public propertyErrorMessageGets or sets the non-localizable error message to display when validation fails. (Inherited from ValidationAttribute.)
Public propertyErrorMessageResourceNameGets or sets the property name on the resource type that provides the localizable error message. (Inherited from ValidationAttribute.)
Public propertyErrorMessageResourceTypeGets or sets the resource type that provides the localizable error message. (Inherited from ValidationAttribute.)
Protected propertyErrorMessageStringGets the localized or non-localized error message. (Inherited from ValidationAttribute.)
Public propertyPatternGets the regular expression to use for validation of the associated member.
Top

  NameDescription
Public methodEqualsInfrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodFormatErrorMessageApplies formatting to an error message based on the data field where the error occurred. (Overrides ValidationAttribute.FormatErrorMessage(String).)
Public methodGetHashCodeReturns the hash code for this instance. (Inherited from Attribute.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodGetValidationResultDetermines whether the specified object is valid and returns an object that includes the results of the validation check. (Inherited from ValidationAttribute.)
Protected methodIsValidDetermines whether the specified object is valid. (Inherited from ValidationAttribute.)
Public methodMatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodValidateDetermines whether the specified object is valid and throws a ValidationException if the object is not valid. (Inherited from ValidationAttribute.)
Top

You apply the RegularExpressionAttribute attribute to a property when you need to validate values for the property against a regular expression. The regular expression enables you to specify very precisely the format of valid values. The Pattern property contains the regular expression.

If the value of the property is Nothing or an empty string (""), the value automatically passes validation for the RegularExpressionAttribute attribute. To validate that the value is not Nothing 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 Property LastName() As String
        'Implement Get and Set logic
    End Property
End Class


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.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

ADD
Show:
© 2014 Microsoft