This documentation is archived and is not being maintained.

RegexStringValidator Class

Provides validation of a string based on the rules provided by a regular expression.

Namespace:  System.Configuration
Assembly:  System.Configuration (in System.Configuration.dll)

'Declaration
Public Class RegexStringValidator _
	Inherits ConfigurationValidatorBase
'Usage
Dim instance As RegexStringValidator

The RegexStringValidator object contains the rules necessary to validate a string object based on a regular expression. The rules are established when an instance of the RegexStringValidator class is created.

The CanValidate method determines whether the object type being validated matches the expected type. The object being validated is passed as a parameter of the Validate method.

The following code example demonstrates how to use the RegexStringValidator type.

Imports System
Imports System.Configuration

Namespace Microsoft.Samples.AspNet.Validators
  Class UsingRegexStringValidator
    Public Shared Sub Main()

      ' Display title.
      Console.WriteLine("ASP.NET Validators")
      Console.WriteLine()

      ' Create RegexString and Validator. 
      Dim testString As String = "someone@example.com" 
      Dim regexString As String = _
       "^[a-zA-Z\.\-_]+@([a-zA-Z\.\-_]+\.)+[a-zA-Z]{2,4}$" 
      Dim myRegexValidator As RegexStringValidator = _ 
       New RegexStringValidator(regexString)

      ' Determine if the object to validate can be validated.
      Console.WriteLine("CanValidate: {0}", _
        myRegexValidator.CanValidate(testString.GetType()))

      Try 
        ' Attempt validation.
        myRegexValidator.Validate(testString)
        Console.WriteLine("Validated.")

      Catch e As Exception
        ' Validation failed.
        Console.WriteLine("Error: {0}", e.Message.ToString())
      End Try 

      ' Display and wait.
      Console.ReadLine()
    End Sub 
  End Class 
End Namespace

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Show: