RegexStringValidator Klasa

Definicja

Zapewnia walidację ciągu na podstawie reguł dostarczonych przez wyrażenie regularne.

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

Przykłady

W poniższym przykładzie kodu pokazano, jak używać RegexStringValidator typu.

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.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

Uwagi

Obiekt RegexStringValidator zawiera reguły niezbędne do zweryfikowania obiektu ciągu na podstawie wyrażenia regularnego. Reguły są ustanawiane podczas tworzenia wystąpienia tej klasy.

Metoda CanValidate określa, czy typ obiektu, który jest weryfikowany, jest zgodny z oczekiwanym typem. Obiekt do weryfikacji jest przekazywany jako argument do Validate metody .

Konstruktory

RegexStringValidator(String)

Inicjuje nowe wystąpienie klasy RegexStringValidator.

Metody

CanValidate(Type)

Określa, czy można zweryfikować typ obiektu.

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)
Validate(Object)

Weryfikuje ciąg na podstawie reguł dostarczonych przez wyrażenie regularne.

Dotyczy

Zobacz też