Regular Expression Validator

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

Class Name: RegexValidator

Attribute Name: RegexValidatorAttribute

Configuration tool name: Regular Expression Validator

Description

This validator checks that the value matches the pattern given by a regular expression.

Properties

The following table lists the Regular Expression Validator properties.

Regular Expression Validator Properties

Property

Description

Options

This property sets the option to use when validating a pattern. Possible values are None, IgnoreCase, Multiline, ExplicitCapture, Compiled, Singleline, IgnorePatternWhiteSpace, and RighttoLeft. The default is None.

Pattern

This property defines the regular expression. It is required. You can either enter the expression or use the Regular Expression Editor dialog box.

PatternResourceName

If you do not want to use the Pattern property to specify a regular expression, you can use another pattern by specifying it with the PatternResourceName value. If you include both a Pattern value and a PatternResourceName value, the Pattern value takes precedence.

PatternResourceTypeName

If you specify a PatternResourceName value then you must specify a PatternResourceTypeName value, which is the type of the template you want to use.

Tag

This property is a user-supplied string. Typically, it is used to sort or categorize validation results that are in a log.

MessageTemplate

This property is a user-supplied string. Typically, it describes the validation result and is intended for a log.

MessageTemplateResourceName

If you do not want to use the MessageTemplate property to specify a message, you can use another template by specifying it with the MessageTemplateResourceName value. If you include both a MessageTemplate value and a MessageTemplateResourceName value, the MessageTemplate value takes precedence.

MessageTemplateResourceTypeName

If you specify a MessageTemplateResourceName value then you must specify a MessageTemplateResourceTypeName value, which is the type of the template you want to use.

Message Template Tokens

If the message template contains tokens (for instance, "{0}"), the validator will replace these tokens with values when the ValidationResult is created. The tokens supported by the RegexValidator are as follows:

Token

Meaning

{3}

The regular expression pattern configured for the validator instance.

{4}

The regular expression options configured for the validator instance.

Examples

The following examples show how to use the validator with attributes and with code.

Attributes

The following code example checks that the e-mail address is formed according to the pattern given by the regular expression.

public class Person
{
   [RegexValidator(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")]
   public string EmailAddress
   {
      get
      {
         return emailAddress;
      }
   }
// ...
}
'Usage
Public Class Person
    <RegexValidator("\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")> _
    ReadOnly Property EmailAddress(ByVal _emailAddress As String)
        Get
            Return _emailAddress
        End Get
    End Property
' ...
End Class
Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.