CustomValidator クラスとは? わかりやすく解説

CustomValidator クラス

他のコントロールに対してカスタム検証実行できるコントロール提供します

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

Public Class CustomValidator
    Inherits BaseValidator
Dim instance As CustomValidator
public class CustomValidator : BaseValidator
public ref class CustomValidator : public
 BaseValidator
public class CustomValidator extends BaseValidator
public class CustomValidator extends
 BaseValidator
解説解説
使用例使用例

ユーザーTextBox コントロール入力した値が偶数かどうかチェックする例を次に示します。値が偶数である場合ページは有効と見なされます。値が偶数ない場合ページ無効と見なされ、CustomValidator によって ErrorMessage プロパティ表示されます。

メモメモ

次のコード サンプルはシングルファイル コード モデル使用しており、分離コード ファイル直接コピーされ場合正常に動作しない可能性あります。このコード サンプルは、拡張子.aspx の空のテキスト ファイルコピーする必要がありますWeb フォームコード モデル詳細については、「ASP.NET Web ページコード モデル」を参照してください

<%@ Page Language="VB" 
    Inherits="System.Web.UI.MobileControls.MobilePage"
 %>
<%@ Register TagPrefix="mobile" 
    Namespace="System.Web.UI.MobileControls"
 
    Assembly="System.Web.Mobile"
 %>

<script runat="server">
    ' If the page validates, go to page 2
    Protected Sub Submit_Click(ByVal
 sender As Object, ByVal
 e As EventArgs)
        If (Page.IsValid) Then
            ActiveForm = Form2
        End If
    End Sub
    
    ' Validate whether the number is even
    Private Sub ServerValidate(ByVal
 source As Object, _
        ByVal args As ServerValidateEventArgs)
    
        ' Convert the text to a number
        Dim num As Integer
        Integer.TryParse(numberBox.Text, num)
        ' Test for an even number
        If (num > 0) Then
            args.IsValid = ((num Mod 2) = 0)
        Else
            args.IsValid = False
        End If
    End Sub

</script>

<html  >
<body>
    <mobile:form id="Form1" runat="server">
        <mobile:Label ID="Label1" runat="server">
            Please enter an even number greater than zero.
        </mobile:Label>
        <mobile:TextBox ID="numberBox" Runat="server"
 
            Numeric="true" MaxLength="2"
 />
        <mobile:CustomValidator ID="CustomValidator1"
 
            ControlToValidate="numberBox"
            OnServerValidate="ServerValidate" runat="server">
            Your number is not an even number.
        </mobile:CustomValidator>
        <mobile:Command ID="Command1" runat="server"
 
            OnClick="Submit_Click">
            Submit
        </mobile:Command>
    </mobile:form>
    <mobile:Form id="Form2" runat="server">
        <mobile:Label ID="Label2" runat="server">
            Your number is an even number.
        </mobile:Label>
    </mobile:Form>
</body>
</html>
<%@ Page Language="C#" 
    Inherits="System.Web.UI.MobileControls.MobilePage" %>
<%@ Register TagPrefix="mobile" 
    Namespace="System.Web.UI.MobileControls" 
    Assembly="System.Web.Mobile" %>

<script runat="server">
    // If the page validates, go to page 2
    protected void Submit_Click(Object sender,
 EventArgs e)
    {
        if (Page.IsValid)
        {
            ActiveForm = Form2;
        }
    }
    
    // Validate whether the number is even
    private void ServerValidate(object source,
 
        ServerValidateEventArgs args)
    {
        // Convert the text to a number
        int num;
        Int32.TryParse(numberBox.Text, out num);
        // Test for an even number
        if (num > 0)
            args.IsValid = ((num % 2) == 0);
        else
            args.IsValid = false;
    }
</script>

<html  >
<body>
    <mobile:form id="Form1" runat="server">
        <mobile:Label ID="Label1" runat="server">
            Please enter an even number greater than zero.
        </mobile:Label>
        <mobile:TextBox ID="numberBox" Runat="server" 
            Numeric="true" MaxLength="2" />
        <mobile:CustomValidator ID="CustomValidator1" 
            ControlToValidate="numberBox"
            OnServerValidate="ServerValidate" runat="server">
            Your number is not an even number.
        </mobile:CustomValidator>
        <mobile:Command ID="Command1" runat="server" 
            OnClick="Submit_Click">
            Submit
        </mobile:Command>
    </mobile:form>
    <mobile:Form id="Form2" runat="server">
        <mobile:Label ID="Label2" runat="server">
            Your number is an even number.
        </mobile:Label>
    </mobile:Form>
</body>
</html>
.NET Framework のセキュリティ.NET Frameworkセキュリティ
継承階層継承階層
System.Object
   System.Web.UI.Control
     System.Web.UI.MobileControls.MobileControl
       System.Web.UI.MobileControls.TextControl
         System.Web.UI.MobileControls.BaseValidator
          System.Web.UI.MobileControls.CustomValidator
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
CustomValidator メンバ
System.Web.UI.MobileControls 名前空間
その他の技術情報
CustomValidator コントロール概要
CustomValidator コントロール追加構成

CustomValidator クラス

入力コントロールユーザー定義検証実行します

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

Public Class CustomValidator
    Inherits BaseValidator
Dim instance As CustomValidator
public class CustomValidator : BaseValidator
public ref class CustomValidator : public
 BaseValidator
public class CustomValidator extends BaseValidator
public class CustomValidator extends
 BaseValidator
解説解説

CustomValidator コントロール使用して入力コントロールユーザー定義検証関数提供しますCustomValidator コントロール検証対象入力コントロールとは別のコントロールで、検証メッセージ表示する位置制御できます

検証コントロールは常にサーバー上で検証実行します検証コントロールクライアント側でも実装でき、スクリプト対応ブラウザ (Microsoft Internet Explorer 4.0 以降など) ではクライアント上で検証実行できますクライアント側検証で、ユーザーによる入力サーバー送信する前にチェックすることによって、検証プロセス強化されます。これによりフォーム送信前にクライアントエラー検出できるため、サーバー側での検証必要な情報ラウンド トリップ避けることができます

サーバー検証関数作成するには、検証実行する ServerValidate イベント用のハンドラ作成します検証対象入力コントロール文字列には、パラメータとしてイベント ハンドラ渡された ServerValidateEventArgs オブジェクトValue プロパティ使用してアクセスできます検証結果は、その後 ServerValidateEventArgs オブジェクトの IsValid プロパティ格納されます。

クライアント側検証関数作成するには、最初に前のサーバー検証関数追加します次に ASP.NET (.aspx) ページクライアント側検証スクリプト関数追加します

VBScript (Visual Basic Scripting Edition) を使用している場合関数次の形式にする必要があります

 Sub ValidationFunctionName(source, arguments)

JScript使用している場合関数次の形式にする必要があります

 function ValidationFunctionName(source, arguments)

ClientValidationFunction プロパティ使用してCustomValidator コントロール関連付けられているクライアント側検証スクリプト関数の名前を指定しますスクリプト関数クライアント実行されるため、対象ブラウザサポートする言語 (VBScriptJScript など) を使用する必要があります

サーバー側の検証と同様、検証対象入力コントロール文字列には arguments パラメータValue プロパティ使用してアクセスます。arguments パラメータIsValid プロパティ設定することにより検証結果返します

注意に関するメモメモ

入力コントロールが空の場合検証関数呼び出されず、検証成功しますRequiredFieldValidator コントロール使用すると、データ入力コントロール入力するようにユーザー要求します

ControlToValidate プロパティ設定せずCustomValidator コントロール使用できます。これは、通常複数入力コントロール検証するとき、または CheckBox コントロールなどの検証コントロール使用できない入力コントロール検証するときに実行します。この場合ServerValidate イベントイベント ハンドラクライアント側検証関数渡される arguments パラメータValue プロパティには、常に空の文字列 ("") が含まれます。ただし、サーバークライアント両方有効性確認するために、これらの検証関数必要に応じて呼び出される場合あります検証する値にアクセスするには、検証対象入力コントロールプログラムにより参照し適切なプロパティから値を取得します。たとえば、サーバーCheckBox コントロール検証する場合には、検証コントロールControlToValidate プロパティ設定せずServerValidate イベントハンドラのために次のコード使用します

Sub ServerValidation (source As object,
 args As ServerValidateEventArgs)
    args.IsValid = (CheckBox1.Checked = True)
 End Sub
void ServerValidation (object source, ServerValidateEventArgs
 args)
 {
    args.IsValid = (CheckBox1.Checked == true);
 }

検証コントロール詳細については、「BaseValidator」を参照してください

ユーザー補助
TopicLocation
チュートリアル : Web フォーム ページにおけるユーザー入力検証Visual Studio での ASP .NET Web アプリケーション作成
方法 : ASP.NET サーバー コントロール必要なエントリ検証するASP .NET Web アプリケーション作成
方法 : ASP.NET サーバー コントロールカスタム検証メッセージ表示するASP .NET Web アプリケーション作成
方法 : ASP.NET サーバー コントロールデータベースの値を検証するASP .NET Web アプリケーション作成
方法 : ASP.NET サーバー コントロールデータ型検証するASP .NET Web アプリケーション作成
方法 : ASP.NET サーバー コントロールパターンに対して検証するASP .NET Web アプリケーション作成
方法 : ASP.NET サーバー コントロールの値の範囲検証するASP .NET Web アプリケーション作成
方法 : ASP.NET サーバー コントロール固有の値を検証するASP .NET Web アプリケーション作成
方法 : ASP.NET サーバー コントロール埋め込みメッセージレイアウト指定するASP .NET Web アプリケーション作成
方法 : ASP.NET サーバー コントロール有効性プログラムテストするASP .NET Web アプリケーション作成
方法 : ASP.NET サーバー コントロール検証エラー メッセージ書式設定するASP .NET Web アプリケーション作成
方法 : ASP.NET サーバー コントロール検証無効にするASP .NET Web アプリケーション作成
方法 : ASP.NET サーバー コントロールカスタム関数検証するASP .NET Web アプリケーション作成
方法 : ASP.NET サーバー コントロールプログラム検証するASP .NET Web アプリケーション作成
使用例使用例

サーバー側の CustomValidator コントロール作成する方法コード例次に示します

<%@ Page Language="VB" AutoEventWireup="True"
 %>

<html>
<head>

   <script runat="server">

      Sub ValidateBtn_OnClick(sender As Object,
 e As EventArgs) 

         ' Display whether the page passed validation.
         If Page.IsValid Then 

            Message.Text = "Page is valid."

         Else 

            Message.Text = "Page is not valid!"

         End If

      End Sub

      Sub ServerValidation(source As Object,
 args As ServerValidateEventArgs)

         Try 

            ' Test whether the value entered into the text box is even.
            Dim num As Integer
 = Integer.Parse(args.Value)
            args.IsValid = ((num mod 2) = 0)
 
         Catch ex As Exception
         
            args.IsValid = false

         End Try

      End Sub

   </script>    

</head>
<body>

   <form runat="server">
  
      <h3>CustomValidator ServerValidate Example</h3>

      <asp:Label id="Message"
           Text="Enter an even number:" 
           Font-Name="Verdana" 
           Font-Size="10pt" 
           runat="server" />

      <p>

      <asp:TextBox id="Text1" 
           runat="server" />
    
      &nbsp;&nbsp;

      <asp:CustomValidator id="CustomValidator1"
           ControlToValidate="Text1"
           Display="Static"
           ErrorMessage="Not an even number!"
           ForeColor="green"
           Font-Name="verdana" 
           Font-Size="10pt"
           OnServerValidate="ServerValidation"
           runat="server"/>

      <p>
 
      <asp:Button id="Button1"
           Text="Validate" 
           OnClick="ValidateBtn_OnClick" 
           runat="server"/>

   </form>
  
</body>
</html>

<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
<head>

   <script runat="server">

      void ValidateBtn_OnClick(object sender, EventArgs e) 
      { 

         // Display whether the page passed validation.
         if (Page.IsValid) 
         {

            Message.Text = "Page is valid.";

         }

         else 
         {

            Message.Text = "Page is not valid!";

         }

      }

      void ServerValidation(object source, ServerValidateEventArgs
 args)
      {

         try 
         {

            // Test whether the value entered into the text box is even.
            int i = int.Parse(args.Value);
            args.IsValid = ((i%2) == 0);

         }

         catch(Exception ex)
         {

            args.IsValid = false;

         }

      }

   </script>    

</head>
<body>

   <form runat="server">
  
      <h3>CustomValidator ServerValidate Example</h3>

      <asp:Label id="Message"  
           Text="Enter an even number:" 
           Font-Name="Verdana" 
           Font-Size="10pt" 
           runat="server"/>

      <p>

      <asp:TextBox id="Text1" 
           runat="server" />
    
      &nbsp;&nbsp;

      <asp:CustomValidator id="CustomValidator1"
           ControlToValidate="Text1"
           Display="Static"
           ErrorMessage="Not an even number!"
           ForeColor="green"
           Font-Name="verdana" 
           Font-Size="10pt"
           OnServerValidate="ServerValidation"
           runat="server"/>

      <p>
 
      <asp:Button id="Button1"
           Text="Validate" 
           OnClick="ValidateBtn_OnClick" 
           runat="server"/>

   </form>
  
</body>
</html>

クライアント側CustomValidator コントロール作成する方法コード例次に示します

<%@ Page Language="VB" AutoEventWireup="True"
 %>

<html>
<head>

   <script runat="server">

      Sub ValidateBtn_OnClick(sender As Object,
 e As EventArgs) 

         ' Display whether the page passed validation.
         If Page.IsValid Then 

            Message.Text = "Page is valid."

         Else 

            Message.Text = "Page is not valid!"

         End If

      End Sub

      Sub ServerValidation(source As Object,
 args As ServerValidateEventArgs)

         Try 

            ' Test whether the value entered into the text box is even.
            Dim num As Integer
 = Integer.Parse(args.Value)
            args.IsValid = ((num mod 2) = 0)
 
         Catch ex As Exception
         
            args.IsValid = false

         End Try

      End Sub

   </script>      

</head>
<body>

   <form runat="server">
  
      <h3>CustomValidator ServerValidate Example</h3>

      <asp:Label id="Message"  
           Text="Enter an even number:" 
           Font-Name="Verdana" 
           Font-Size="10pt" 
           runat="server"/>

      <p>

      <asp:TextBox id="Text1" 
           runat="server" />
    
      &nbsp;&nbsp;

      <asp:CustomValidator id="CustomValidator1"
           ControlToValidate="Text1"
           ClientValidationFunction="ClientValidate"
           OnServerValidate="ServerValidation"
           Display="Static"
           ErrorMessage="Not an even number!"
           ForeColor="green"
           Font-Name="verdana" 
           Font-Size="10pt"
           runat="server"/>

      <p>
 
      <asp:Button id="Button1"
           Text="Validate" 
           OnClick="ValidateBtn_OnClick" 
           runat="server"/>

   </form>
  
</body>
</html>

<script language="vbscript">

   <!--

   Sub ClientValidate(source, arguments)
            
      If (arguments.Value mod 2) = 0 Then
         arguments.IsValid=true
      Else
         arguments.IsValid=false
      End If

   End Sub

   ' -->

</script>


<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
<head>

   <script runat="server">

      void ValidateBtn_OnClick(object sender, EventArgs e) 
      { 

         // Display whether the page passed validation.
         if (Page.IsValid) 
         {

            Message.Text = "Page is valid.";

         }

         else 
         {

            Message.Text = "Page is not valid!";

         }

      }

      void ServerValidation(object source, ServerValidateEventArgs
 args)
      {

         try 
         {

            // Test whether the value entered into the text box is even.
            int i = int.Parse(args.Value);
            args.IsValid = ((i%2) == 0);

         }

         catch(Exception ex)
         {

            args.IsValid = false;

         }

      }

   </script>    

</head>
<body>

   <form runat="server">
  
      <h3>CustomValidator ServerValidate Example</h3>

      <asp:Label id="Message"  
           Text="Enter an even number:" 
           Font-Name="Verdana" 
           Font-Size="10pt" 
           runat="server"/>

      <p>

      <asp:TextBox id="Text1" 
           runat="server" />
    
      &nbsp;&nbsp;

      <asp:CustomValidator id="CustomValidator1"
           ControlToValidate="Text1"
           ClientValidationFunction="ClientValidate"
           OnServerValidate="ServerValidation"
           Display="Static"
           ErrorMessage="Not an even number!"
           ForeColor="green"
           Font-Name="verdana" 
           Font-Size="10pt"
           runat="server"/>

      <p>
 
      <asp:Button id="Button1"
           Text="Validate" 
           OnClick="ValidateBtn_OnClick" 
           runat="server"/>

   </form>
  
</body>
</html>

<script language="vbscript">

   <!--

   Sub ClientValidate(source, arguments)
            
      If (arguments.Value mod 2) = 0 Then
         arguments.IsValid=true
      Else
         arguments.IsValid=false
      End If

   End Sub

   ' -->

</script>


.NET Framework のセキュリティ.NET Frameworkセキュリティ
継承階層継承階層
System.Object
   System.Web.UI.Control
     System.Web.UI.WebControls.WebControl
       System.Web.UI.WebControls.Label
         System.Web.UI.WebControls.BaseValidator
          System.Web.UI.WebControls.CustomValidator
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
CustomValidator メンバ
System.Web.UI.WebControls 名前空間
BaseValidator クラス
RequiredFieldValidator
ClientValidationFunction
ServerValidate
OnServerValidate
ServerValidateEventArgs
Value
IsValid



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「CustomValidator クラス」の関連用語











CustomValidator クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



CustomValidator クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS