次の方法で共有


CustomValidator.ClientValidationFunction プロパティ

検証に使用するカスタム クライアント側スクリプト関数の名前を取得または設定します。

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

構文

'宣言
<ThemeableAttribute(False)> _
Public Property ClientValidationFunction As String
'使用
Dim instance As CustomValidator
Dim value As String

value = instance.ClientValidationFunction

instance.ClientValidationFunction = value
[ThemeableAttribute(false)] 
public string ClientValidationFunction { get; set; }
[ThemeableAttribute(false)] 
public:
property String^ ClientValidationFunction {
    String^ get ();
    void set (String^ value);
}
/** @property */
public String get_ClientValidationFunction ()

/** @property */
public void set_ClientValidationFunction (String value)
public function get ClientValidationFunction () : String

public function set ClientValidationFunction (value : String)
適用できません。

プロパティ値

検証に使用するカスタム クライアント スクリプト関数の名前。既定値は String.Empty で、このプロパティが設定されていないことを示します。

メモメモ :

関数名にはかっこやパラメータは使用できません。

解説

このプロパティをクライアント側検証の実行関数の名前に設定します。

クライアントの検証関数は対象となるブラウザ上で実行されるため、この関数は JScript や VBScript などブラウザがサポートするスクリプト言語を使用して書く必要があります。

このプロパティは、テーマまたはスタイル シート テーマによって設定することはできません。詳細については、ThemeableAttributeASP.NET のテーマとスキンの概要 の各トピックを参照してください。

トピック 場所
チュートリアル : Web フォーム ページにおけるユーザーの入力の検証 Visual Studio での ASP .NET Web アプリケーションの作成
チュートリアル : Web フォーム ページにおけるユーザーの入力の検証 Visual Web Developer でのアプリケーションの作成

使用例

ClientValidationFunction プロパティを使用して、クライアント側検証の実行関数の名前を指定する方法のコード例を次に示します。検証関数は偶数をチェックします。

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

この例には、ユーザー入力を受け付けるテキスト ボックスがあります。これにより、セキュリティが脆弱になる可能性があります。既定では、ASP.NET Web ページによって、ユーザー入力にスクリプトまたは HTML 要素が含まれていないかどうかが検証されます。詳細については、「スクリプトによる攻略の概要」を参照してください。

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    Sub ValidateBtn_OnClick(ByVal sender As Object, ByVal 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(ByVal source As Object, ByVal 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>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>CustomValidator Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:Label id="Message"  
           Text="Enter an even number:" 
           Font-Size="10pt" 
           runat="server"
           AssociatedControlID="Text1"/>
      <br />
      <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-Size="10pt"
           runat="server"/>
      <br /> 
      <asp:Button id="Button1"
           Text="Validate" 
           OnClick="ValidateBtn_OnClick" 
           runat="server"/>    
    </div>
    </form>
</body>
</html>
<script type="text/javascript">
   function ClientValidate(source, clientside_arguments)
   {         
      if (clientside_arguments.Value % 2 == 0 )
      {
         clientside_arguments.IsValid=true;
      }
      else {clientside_arguments.IsValid=false};
   }
</script>
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<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>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>CustomValidator Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:Label id="Message"  
           Text="Enter an even number:" 
           Font-Size="10pt" 
           runat="server"
           AssociatedControlID="Text1"/>
      <br />
      <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-Size="10pt"
           runat="server"/>
      <br /> 
      <asp:Button id="Button1"
           Text="Validate" 
           OnClick="ValidateBtn_OnClick" 
           runat="server"/>    
    </div>
    </form>
</body>
</html>
<script type="text/javascript">
   function ClientValidate(source, clientside_arguments)
   {         
      if (clientside_arguments.Value % 2 == 0 )
      {
         clientside_arguments.IsValid=true;
      }
      else {clientside_arguments.IsValid=false};
   }
</script>

プラットフォーム

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

参照

関連項目

CustomValidator クラス
CustomValidator メンバ
System.Web.UI.WebControls 名前空間
String.Empty