次の方法で共有


IValidator インターフェイス

Web フォームの検証に参加するオブジェクトが実装する必要があるプロパティとメソッドを定義します。

名前空間: System.Web.UI
アセンブリ: System.Web (system.web.dll 内)

構文

'宣言
Public Interface IValidator
'使用
Dim instance As IValidator
public interface IValidator
public interface class IValidator
public interface IValidator
public interface IValidator
適用できません。

解説

このインターフェイスを実装しているクラスは、発生する可能性があるユーザー入力エラーを表します。Validate メソッドが呼び出されると、このクラスは、その IsValid プロパティを更新して、エラーが発生したかどうかを示します。ErrorMessage プロパティには、エラー発生時に表示できるエラー条件の説明テキストが格納されています。

BaseValidator クラスはこのインターフェイスを実装し、その他の ASP.NET 検証サーバー コントロールのクラスはすべて BaseValidator から継承します。検証サーバー コントロールの詳細と動作については、「検証 ASP.NET コントロール」を参照してください。

カスタム ASP.NET 検証サーバー コントロールを開発する方法については、「方法 : ASP.NET サーバー コントロールをカスタム関数で検証する」を参照してください。

使用例

セキュリティに関するメモセキュリティに関するメモ :

この例には、ユーザー入力を受け付けるテキスト ボックスがあります。これにより、セキュリティが脆弱になる可能性があります。既定では、ASP.NET Web ページによって、ユーザー入力にスクリプトまたは 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_number As New Random()
            myCompareValidate.ValueToCompare = rand_number.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>
<!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_number = new Random();
        myCompareValidate.ValueToCompare = rand_number.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="VJ#" runat="server">

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

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

        // Check for Validity of control.
        if (myCompareValidate.get_IsValid() 
            && (!(myTextBox.get_Text().Equals("")))) {
            labelOutput.set_Text("You guessed correctly!!");
            labelOutput.set_ForeColor(System.Drawing.Color.get_Blue());
        }
        else {
            labelOutput.set_Text("You guessed poorly");
            labelOutput.set_ForeColor(System.Drawing.Color.get_Black());
        }
        labelOutput.set_Text(labelOutput.get_Text() + "<br /><br />" 
            + "The number is: " + myCompareValidate.get_ValueToCompare());
    } //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 ID="Button1" Text="Submit" OnClick="Button_Click" runat="server" />
        <br />
        <asp:Label id="labelOutput" runat="server" />
        <br />
        <asp:ValidationSummary id="Summary1" runat="server" />
     </form>
  </body>
</html>

プラットフォーム

Windows 98,Windows Server 2000 SP4,Windows CE,Windows Millennium Edition,Windows Mobile for Pocket PC,Windows Mobile for Smartphone,Windows Server 2003,Windows XP Media Center Edition,Windows XP Professional x64 Edition,Windows XP SP2,Windows XP Starter Edition

Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。

バージョン情報

.NET Framework

サポート対象 : 3.0,2.0,1.1,1.0

参照

関連項目

IValidator メンバ
System.Web.UI 名前空間
ValidatorCollection
Validators
BaseValidator
BaseCompareValidator
CompareValidator
CustomValidator
RangeValidator
RegularExpressionValidator
RequiredFieldValidator

その他の技術情報

方法 : ASP.NET サーバー コントロールをカスタム関数で検証する
検証 ASP.NET コントロール