IValidator 接口

定义

定义参与 Web 窗体验证的对象必须实现的属性和方法。Defines the properties and methods that objects that participate in Web Forms validation must implement.

public interface class IValidator
public interface IValidator
type IValidator = interface
Public Interface IValidator
派生

示例

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。This example has a text box that accepts user input, which is a potential security threat. 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 有关详细信息,请参阅脚本侵入概述For more information, see Script Exploits Overview.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>IValidator Example demonstrating IsValid & ErrorMessage</title>
<script language="C#" runat="server">

     void Button_Click(Object sender, EventArgs e)
     {
        // Generating the random number.
        Random rand_s = new Random();
        myCompareValidate.ValueToCompare = rand_s.Next(1, 10).ToString();

        // Setting the ErrorMessage.
        myCompareValidate.ErrorMessage="Try Again!!";
        myCompareValidate.Validate();

        // Check for Validity of control.
        if ((myCompareValidate.IsValid) && (myTextBox.Text != ""))
        {
           labelOutput.Text = "You guessed correctly!!";
           labelOutput.ForeColor = System.Drawing.Color.Blue;
        }
        else
        {
           labelOutput.Text =  "You guessed poorly";
           labelOutput.ForeColor = System.Drawing.Color.Black;
        }

        labelOutput.Text += "<br /><br />" + "The number is: " +
           myCompareValidate.ValueToCompare;
     }

  </script>
</head>
    <body>
      <form runat="server" id="myForm">
        <h3>IValidator Example demonstrating IsValid & ErrorMessage</h3>
        <h5>Guess!! a number between 1 and 10 :</h5>
        <asp:TextBox id="myTextBox" runat="server" />
        <asp:CompareValidator id="myCompareValidate"
             ControlToValidate="myTextBox" ValueToCompare="0"
             EnableClientScript="False" Type="Integer" Text="*"
             runat="server" />
        <br />
        <asp:Button Text="Submit" OnClick="Button_Click" runat="server" />
        <br />
        <asp:Label id="labelOutput" runat="server" />
        <br />
        <asp:ValidationSummary id="Summary1" runat="server" />
     </form>
  </body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
   <head>
    <title>IValidator Example demonstrating IsValid & ErrorMessage</title>
<script language="VB" runat="server">

         Sub Button_Click(sender As [Object], e As EventArgs)
            ' Generating a random number.
            Dim rand_s As New Random()
            myCompareValidate.ValueToCompare = rand_s.Next(1, 10).ToString()

            ' Set the ErrorMessage.
            myCompareValidate.ErrorMessage = "Try Again!!"
            myCompareValidate.Validate()

            ' Check for Validity of control.
            If myCompareValidate.IsValid And myTextBox.Text <> "" Then
               labelOutput.Text = "You guessed correctly!!"
               labelOutput.ForeColor = System.Drawing.Color.Blue
            Else
               labelOutput.Text = "You guessed poorly"
               labelOutput.ForeColor = System.Drawing.Color.Black
            End If

            labelOutput.Text += "<br /><br />" + "The number is: " + _
               myCompareValidate.ValueToCompare
         End Sub 'Button_Click

  </script>
</head>
    <body>
       <form runat="server" id="myForm">
          <h3>IValidator Example demonstrating IsValid & ErrorMessage</h3>
          <h5>Guess!! a number between 1 and 10:</h5>
          <asp:TextBox id="myTextBox" runat="server" />
          <asp:CompareValidator id="myCompareValidate"
               ControlToValidate="myTextBox" ValueToCompare="0"
               EnableClientScript="False" Type="Integer" Text="*"
               runat="server" />
          <br />
          <asp:Button Text="Submit" OnClick="Button_Click" runat="server" />
          <br />
          <asp:Label id="labelOutput" runat="server" />
          <br />
          <asp:ValidationSummary id="Summary1" runat="server" />
       </form>
    </body>
</html>

注解

实现此接口的类表示可能的用户输入错误。Classes that implement this interface represent a possible user input error. 调用方法时, 类会更新其IsValid属性, 以表示是否发生了错误。 ValidateWhen the Validate method is called, the class updates its IsValid property to signify whether the error occurred. ErrorMessage属性包含错误条件的文本说明, 您可以在发生错误时显示这些说明。The ErrorMessage property contains a text description of the error condition that you can display when the error occurs.

类实现此接口, 所有其他 ASP.NET 验证服务器控件类都继承自BaseValidatorBaseValidatorThe BaseValidator class implements this interface, and all other ASP.NET validation server control classes inherit from BaseValidator. 有关验证服务器控件及其工作原理的信息, 请参阅ASP.NET 验证控件For information on validation server controls and how they work, see ASP.NET Validation Controls.

有关如何开发自定义 ASP.NET 验证服务器控件的详细信息, 请参阅如何:使用 ASP.NET 服务器控件的自定义函数进行验证。For details on how to develop custom ASP.NET validation server controls, see How to: Validate with a Custom Function for ASP.NET Server Controls.

属性

ErrorMessage

由类实现时,获取或设置条件验证失败时生成的错误消息。When implemented by a class, gets or sets the error message text generated when the condition being validated fails.

IsValid

由类实现时,获取或设置一个值,该值指示用户输入的指定控件内容是否通过验证。When implemented by a class, gets or sets a value indicating whether the user-entered content in the specified control passes validation.

方法

Validate()

由类实现时,评估其检查的条件并更新 IsValid 属性。When implemented by a class, evaluates the condition it checks and updates the IsValid property.

适用于

另请参阅