RegexStringValidator RegexStringValidator RegexStringValidator RegexStringValidator Class

定义

根据正则表达式提供的规则提供字符串验证。Provides validation of a string based on the rules provided by a regular expression.

public ref class RegexStringValidator : System::Configuration::ConfigurationValidatorBase
public class RegexStringValidator : System.Configuration.ConfigurationValidatorBase
type RegexStringValidator = class
    inherit ConfigurationValidatorBase
Public Class RegexStringValidator
Inherits ConfigurationValidatorBase
继承

示例

下面的代码示例演示如何使用RegexStringValidator类型。The following code example demonstrates how to use the RegexStringValidator type.

using System;
using System.Configuration;

namespace Microsoft.Samples.AspNet.Validators
{
  class UsingRegexStringValidator
  {
    static void Main(string[] args)
    {
      // Display title.
      Console.WriteLine("ASP.NET Validators");
      Console.WriteLine();

      // Create RegexString and Validator.
      string testString = "someone@example.com";
      string regexString = 
       @"^[a-zA-Z\.\-_]+@([a-zA-Z\.\-_]+\.)+[a-zA-Z]{2,4}$";
      RegexStringValidator myRegexValidator = 
       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 (ArgumentException e)
      {
        // Validation failed.
        Console.WriteLine("Error: {0}", e.Message.ToString());
      }

      // Display and wait
      Console.ReadLine();
    }
  }
}
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

注解

RegexStringValidator对象包含验证基于正则表达式的字符串对象所需的规则。The RegexStringValidator object contains the rules necessary to validate a string object based on a regular expression. 实例时建立规则RegexStringValidator创建类。The rules are established when an instance of the RegexStringValidator class is created.

CanValidate方法确定要验证的对象类型是否与预期的类型相匹配。The CanValidate method determines whether the object type being validated matches the expected type. 正在验证的对象传递的参数作为Validate方法。The object being validated is passed as a parameter of the Validate method.

构造函数

RegexStringValidator(String) RegexStringValidator(String) RegexStringValidator(String) RegexStringValidator(String)

初始化 RegexStringValidator 类的新实例。Initializes a new instance of the RegexStringValidator class.

方法

CanValidate(Type) CanValidate(Type) CanValidate(Type) CanValidate(Type)

确定是否可以验证该对象的类型。Determines whether the type of the object can be validated.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)
Validate(Object) Validate(Object) Validate(Object) Validate(Object)

确定对象的值是否有效。Determines whether the value of an object is valid.

适用于

另请参阅