IValidator 介面

定義

定義參與 Web Form 驗證的物件必須實作的屬性和方法。

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

範例

重要

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 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>

備註

實作此介面的類別代表可能的使用者輸入錯誤。 Validate呼叫 方法時,類別會更新其 IsValid 屬性,以表示是否發生錯誤。 屬性 ErrorMessage 包含錯誤狀況的文字描述,您可以在發生錯誤時顯示。

類別 BaseValidator 會實作這個介面,而所有其他 ASP.NET 驗證伺服器控件類別都繼承自 BaseValidator。 如需驗證伺服器控制項及其運作方式的相關信息,請參閱 ASP.NET 驗證控件

如需如何開發自定義 ASP.NET 驗證伺服器控制件的詳細資訊,請參閱 如何:使用 ASP.NET 伺服器控制件的自定義函式進行驗證

屬性

ErrorMessage

當由類別實作時,取得或設定條件驗證失敗時所產生的錯誤訊息文字。

IsValid

當由類別實作時,取得或設定數值,表示在指定的控制項中的使用者輸入內容是否通過驗證。

方法

Validate()

當由類別實作時,評估它所檢查的條件並更新 IsValid 屬性。

適用於

另請參閱