StringValidator Classe

Definição

Fornece validação de uma cadeia de caracteres.Provides validation of a string.

public ref class StringValidator : System::Configuration::ConfigurationValidatorBase
public class StringValidator : System.Configuration.ConfigurationValidatorBase
type StringValidator = class
    inherit ConfigurationValidatorBase
Public Class StringValidator
Inherits ConfigurationValidatorBase
Herança

Exemplos

O exemplo a seguir demonstra como usar o StringValidator tipo.The following example demonstrates how to use the StringValidator type.

using System;
using System.Configuration;

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

      // Create string and validator.
      string testVal = "filename";
      StringValidator myStrValidator = new StringValidator(1,8,"$%^");

      // Determine if the object to validate can be validated.
      Console.WriteLine("CanValidate: {0}",
        myStrValidator.CanValidate(testVal.GetType()));

      try
      {
        // Attempt validation.
        myStrValidator.Validate(testVal);
        Console.WriteLine("Validated.");
      }
      catch (ArgumentException e)
      {
        // Validation failed.
        Console.WriteLine("Error: {0}", e.Message.ToString());
      }

      // Display and wait.
      Console.ReadLine();
    }
  }
}
Imports System.Configuration

Namespace Samples.AspNet
  Class UsingStringValidator
    Public Shared Sub Main()

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

      ' Create string and validator.
      Dim testVal As String = "filename"
      Dim myStrValidator As StringValidator = New StringValidator(1, 8, "$%^")

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

      Try
        ' Attempt validation.
        myStrValidator.Validate(testVal)
        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

Comentários

A StringValidator classe é usada para garantir que uma cadeia de caracteres atenda a critérios específicos.The StringValidator class is used to ensure that a string meets specific criteria. Os critérios para validação são estabelecidos quando uma instância da StringValidator classe é criada.The criteria for validation is established when an instance of the StringValidator class is created. Há três sobrecargas de construtor para a StringValidator classe.There are three constructor overloads for the StringValidator class. O StringValidator.StringValidator(Int32) Construtor com um parâmetro verifica o comprimento mínimo aceitável da cadeia de caracteres que está sendo validada.The StringValidator.StringValidator(Int32) constructor with one parameter verifies the minimum acceptable length of the string being validated. O StringValidator.StringValidator(Int32, Int32) Construtor com dois parâmetros garante que a cadeia de caracteres que está sendo verificada esteja de acordo com um comprimento mínimo e um máximo.The StringValidator.StringValidator(Int32, Int32) constructor with two parameters ensures that the string being verified adheres to both a minimum and a maximum length. O StringValidator.StringValidator(Int32, Int32, String) Construtor com três parâmetros verifica os valores mínimo e máximo de comprimento da cadeia de caracteres que está sendo verificada, bem como se os caracteres específicos estão presentes na cadeia de caracteres que está sendo validada.The StringValidator.StringValidator(Int32, Int32, String) constructor with three parameters checks both the minimum and the maximum length values of the string being verified, as well as whether specific characters are present in the string being validated.

O CanValidate método determina se o tipo de objeto que está sendo validado corresponde ao tipo esperado.The CanValidate method determines whether the object type being validated matches the expected type. O objeto que está sendo validado é passado como um parâmetro do Validate método.The object being validated is passed as a parameter of the Validate method.

Construtores

StringValidator(Int32)

Inicializa uma nova instância da classe StringValidator, com base em um parâmetro fornecido.Initializes a new instance of the StringValidator class, based on a supplied parameter.

StringValidator(Int32, Int32)

Inicializa uma nova instância da classe StringValidator, com base nos parâmetros fornecidos.Initializes a new instance of the StringValidator class, based on supplied parameters.

StringValidator(Int32, Int32, String)

Inicializa uma nova instância da classe StringValidator, com base nos parâmetros fornecidos.Initializes a new instance of the StringValidator class, based on supplied parameters.

Métodos

CanValidate(Type)

Determina se um objeto pode ser validado com base no tipo.Determines whether an object can be validated based on type.

Equals(Object)

Determina se o objeto especificado é igual ao objeto atual.Determines whether the specified object is equal to the current object.

(Herdado de Object)
GetHashCode()

Serve como a função de hash padrão.Serves as the default hash function.

(Herdado de Object)
GetType()

Obtém o Type da instância atual.Gets the Type of the current instance.

(Herdado de Object)
MemberwiseClone()

Cria uma cópia superficial do Object atual.Creates a shallow copy of the current Object.

(Herdado de Object)
ToString()

Retorna uma cadeia de caracteres que representa o objeto atual.Returns a string that represents the current object.

(Herdado de Object)
Validate(Object)

Determina se o valor de um objeto é válido.Determines whether the value of an object is valid.

Aplica-se a

Confira também