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) 页中。

如果使用的是 Visual Basic Scripting Edition (VBScript),该函数必须采用下面的形式:

 Sub ValidationFunctionName(source, arguments)

如果使用的是 JScript,则该函数必须采用下面的形式:

 function ValidationFunctionName(source, arguments)

使用 ClientValidationFunction 属性指定与 CustomValidator 控件关联的客户端验证脚本函数的名称。由于该脚本函数在客户端执行,因此该函数必须使用目标浏览器支持的语言(如 VBScript 或 JScript)来编写。

与服务器端验证一样,可以通过使用 arguments 参数的 Value 属性来访问要验证的输入控件中的字符串。通过设置 arguments 参数的 IsValid 属性返回验证结果。

警告

在使用验证程序控件时,应该始终首先检查服务器端验证的结果,然后再执行处理。在回发之后但调用事件方法之前,该页将调用验证程序控件并将它们的结果聚集到 Page.IsValid 属性中。(您还可以使用 Validate 方法显式调用验证程序控件。)在您自己的代码中,应该在处理输入之前,先检查 Page.IsValid 属性是否返回了 true。虽然如果验证检查失败,那么启用脚本的浏览器可能会阻止客户端上发生回发,但您还是应该始终首先在服务器代码中检查 Page.IsValid,然后再处理已验证的数据。

多个验证控件可以与单个输入控件一起使用来验证不同的判据。例如,可以在使用户可以输入要添加到购物车中的货物数量的 TextBox 控件上应用多个验证控件。您可以使用 CustomValidator 控件确保所指定的值小于库存的数量,并使用 RequiredFieldValidator 控件确保用户将值输入到 TextBox 控件中。

提示

如果输入控件为空,则不会调用任何验证函数,并且验证将成功。使用 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

辅助功能

默认情况下,为此控件呈现的标记可能不符合辅助功能标准,例如 Web 内容辅助功能准则 1.0 (WCAG) 优先级 1 准则。有关此控件的辅助功能支持的详细信息,请参见 ASP.NET 控件和辅助功能

主题 位置
演练:验证 Web 窗体页中的用户输入 在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:验证 Web 窗体页中的用户输入 使用 Visual Web Developer 生成应用程序
如何:对照数据库中的值验证 ASP.NET 服务器控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:对照取值范围验证 ASP.NET 服务器控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:对照特定值验证 ASP.NET 服务器控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:对照数据类型验证 ASP.NET 服务器控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:指定 ASP.NET 服务器控件上就地消息的布局 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:验证 ASP.NET 服务器控件的必需项 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:显示 ASP.NET 服务器控件的服务器端自定义验证消息 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:以编程方式测试 ASP.NET 服务器控件的有效性 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:根据模式对 ASP.NET 服务器控件进行验证 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:以编程方式验证 ASP.NET 服务器控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:设置 ASP.NET 服务器控件的验证错误信息的格式 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:使用自定义函数对 ASP.NET 服务器控件进行验证 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:禁用 ASP.NET 服务器控件验证 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:对照数据库中的值验证 ASP.NET 服务器控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:对照取值范围验证 ASP.NET 服务器控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:对照特定值验证 ASP.NET 服务器控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:对照数据类型验证 ASP.NET 服务器控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:指定 ASP.NET 服务器控件上就地消息的布局 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:验证 ASP.NET 服务器控件的必需项 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:显示 ASP.NET 服务器控件的服务器端自定义验证消息 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:以编程方式测试 ASP.NET 服务器控件的有效性 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:根据模式对 ASP.NET 服务器控件进行验证 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:以编程方式验证 ASP.NET 服务器控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:设置 ASP.NET 服务器控件的验证错误信息的格式 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:使用自定义函数对 ASP.NET 服务器控件进行验证 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:禁用 ASP.NET 服务器控件验证 在 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 安全性

继承层次结构

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

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

CustomValidator 成员
System.Web.UI.WebControls 命名空间
BaseValidator 类
RequiredFieldValidator
ClientValidationFunction
ServerValidate
OnServerValidate
ServerValidateEventArgs
Value
IsValid