共用方式為


CustomValidator.ClientValidationFunction 屬性

定義

取得或設定驗證使用的自訂用戶端指令碼函式名稱。

public:
 property System::String ^ ClientValidationFunction { System::String ^ get(); void set(System::String ^ value); };
public string ClientValidationFunction { get; set; }
[System.Web.UI.Themeable(false)]
public string ClientValidationFunction { get; set; }
member this.ClientValidationFunction : string with get, set
[<System.Web.UI.Themeable(false)>]
member this.ClientValidationFunction : string with get, set
Public Property ClientValidationFunction As String

屬性值

用於驗證的自訂用戶端指令碼函式名稱。 預設值為 Empty,表示這個屬性未設定。 函式名稱不可包含任何的括號或參數。

屬性

範例

下列程式碼範例示範如何使用 ClientValidationFunction 屬性來指定執行用戶端驗證之函式的名稱。 驗證函式會檢查偶數。 如需函式參數的描述,請參閱 的一 CustomValidator 節。

重要

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。

<%@ 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 id="Form1" 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" />
    
        

      <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="javascript"> 
   function ClientValidate(source, arguments)
   {
        if (arguments.Value % 2 == 0 ){
            arguments.IsValid = true;
        } else {
            arguments.IsValid = false;
        }
   }
</script>
<%@ 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 id="Form1" 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" />
    
        

      <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="javascript">
   function ClientValidate(source, arguments)
   {
        if (arguments.Value % 2 == 0 ){
            arguments.IsValid = true;
        } else {
            arguments.IsValid = false;
        }
   }
</script>

備註

將此屬性設定為執行用戶端驗證之函式的名稱。

由於用戶端驗證函式會在目標瀏覽器上執行,因此必須使用瀏覽器支援的指令碼語言來撰寫函式,例如 JScript 或 VBScript。

這個屬性無法由佈景主題或樣式表主題設定。 如需詳細資訊,請參閱 ThemeableAttributeASP.NET 主題和麵板

適用於

另請參閱