ASP.net驗證控制之CustomValidator控件

      CustomValidator控件也稱爲自定義驗證控件,通過RequiredFieldValidator控件結合CompareValidator控件、RangeValidator控件或RegularExpressionValidator控件之中的一個或多個就能滿足asp.net開發中的90%以上的驗證要求,但是有一些特殊的驗證用上述控件組合無法達到驗證要求,比如要求用戶填寫一個奇數。爲了滿足一些特殊的驗證要求,在asp.net中還有一個CustomValidator控件,在這個控件中可以自己寫驗證規則。

  CustomValidator類是BaseValidator抽象類,所以CustomValidator控件擁有BaseValidator中定義的屬性,除此之外,CustomValidator控件還有以下常見屬性:

屬性名  說明
ClientValidationFunction  用於在客戶端執行驗證的客戶端函數名
ValidateEmptyText 是否驗證空文本,即當所驗證控件值爲空時時候執行客戶端驗證

 
   CustomValidator控件用於在客戶端驗證的函數有兩個參數,第一個是表示被驗證的控件,第二個表示事件數據。第二個參數有兩個屬性:IsValid用於表示被驗證控件是否通過驗證,Value屬性表示被驗證的控件的值。下面就是一個客戶端驗證函數的例子:

複製代碼
<script type="text/javascript">
    //obj表示被驗證的控件
    //args表示事件數據,args有兩個屬性
    //IsValid指示控件是否通過驗證
    //Value表示被驗證的控件的值
    function CheckEven(obj,args)
    {
      var numberPattern=/\d+/;
      //由於控件的ValidateEmptyText設置爲true
      //所以當控件沒有值時進行客戶端驗證
      if(!numberPattern.test(args.Value))//用javascript進行客戶端正則驗證
      {
        args.IsValid=false;//表示未通過驗證,出現錯誤提示
      }
      else if(args.Value%2==0)
      {
        args.IsValid=true;//表示通過驗證,不出現錯誤提示
      }
      else
      {
        args.IsValid=false;//表示未通過驗證,出現錯誤提示
      }
    }
</script>
複製代碼

 

  除了客戶端驗證之外,在CustomValidator控件中還能自己寫服務器端寫驗證方法,它有一個OnServerValidate事件,同它的客戶端處理函數一樣,處理這個事件的委託也需要兩個參數,第一個是表示被驗證的控件,第二個表示事件數據。第二個參數有兩個屬性:IsValid用於表示被驗證控件是否通過驗證,Value屬性表示被驗證的控件的值。它服務器端驗證方法設置界面如下:

 asp.net夜話之九:驗證控件---(下)

  下面我們用一個例子來說明CustomValidator控件的用法,在這裏例子裏用戶被要求輸入兩個數,一個必須是2的倍數,一個必須是3的倍數。用CustomValidator控件就能很輕鬆完成這個功能。

  下面是前臺代碼:

複製代碼
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CustomValidatorDemo.aspx.cs" Inherits="CustomValidatorDemo"%>
<!DOCTYPE html PUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <title>CustomValidator驗證控件用法的例子</title>
  <script type="text/javascript">
    //obj表示被驗證的控件
    //args表示事件數據,args有兩個屬性
    //IsValid指示控件是否通過驗證
        //Value表示被驗證的控件的值

    function CheckEven(obj,args)
    {
      var numberPattern=/\d+/;
      //由於控件的ValidateEmptyText設置爲true
      //所以當控件沒有值時進行客戶端驗證
      if(!numberPattern.test(args.Value))
      {
        args.IsValid=false;//表示未通過驗證,出現錯誤提示
      }
      else if(args.Value%2==0)
      {
        args.IsValid=true;//表示通過驗證,不出現錯誤提示
      }
      else
      {
        args.IsValid=false;//表示未通過驗證,出現錯誤提示
      }
    }
    function CheckMultiple3(obj,args)
    {
      //由於控件的ValidateEmptyText沒有設置,使用了默認值false
      //所以當控件沒有值時不進行客戶端驗證
      var numberPattern=/\d+/;
      if((!numberPattern.test(args.Value))||(args.Value%3!=0))
      {
        args.IsValid=false;
      }
      else
      {
        args.IsValid=true;
      }
    }
  </script>
</head>
<body>
  <form id="form1" runat="server">
      <div>
          <table border="1" width="600">
              <tr><td colspan="2" align="center">CustomValidator驗證控件用法的例子</td></tr>
              <tr><td>
                填一個3的倍數</td><td>
                <asp:TextBox ID="txtOdd" runat="server"></asp:TextBox>
                <asp:CustomValidator ID="CustomValidator2" runat="server" ControlToValidate="txtOdd"
                     ErrorMessage="請輸入3的倍數" ClientValidationFunction="CheckMultiple3" Display="Dynamic" OnServerValidate="CustomValidator2_ServerValidate"></asp:CustomValidator>
               </td></tr>
              <tr>
                    <td>填一個偶數</td>
                    <td>
                    <asp:TextBox ID="txtEven" runat="server"></asp:TextBox>
                  <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtEven"
                    ErrorMessage="請輸入偶數" ClientValidationFunction="CheckEven" ValidateEmptyText="True" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
                    </td>
                </tr>
              <tr><td>
                <asp:Button ID="Button2" runat="server" Text="提交"/>
              </td>
                <td>
                <input id="Reset3" type="reset" value="重置"/></td>
                </tr>
          </table>
      </div>
  </form>
</body>
</html>
複製代碼

  

  此外,還編寫了服務器端驗證方法,下面是後臺代碼:

複製代碼
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class CustomValidatorDemo:System.Web.UI.Page
{
  protected void Page_Load(object sender,EventArgs e)
  {

  }
  //用於驗證控件值是否爲3的倍數
  protected void CustomValidator2_ServerValidate(object source,ServerValidateEventArgs args)
  {
    System.Text.RegularExpressions.Regex regex=new System.Text.RegularExpressions.Regex(@"\d+");
    //先用正則判斷用戶輸入的是否能轉換成數字
    if(!regex.IsMatch(args.Value))
    {
      args.IsValid=false;//表示驗證不通過
    }
    else
    {
      //如果對3取模爲0就是3的倍數
      args.IsValid=(int.Parse(args.Value)%3==0);
    }
  }
  //用於驗證控件值是否爲偶數
  protected void CustomValidator1_ServerValidate(object source,ServerValidateEventArgs args)
  {
    System.Text.RegularExpressions.Regex regex=new System.Text.RegularExpressions.Regex(@"\d+");
    //先用正則判斷用戶輸入的是否能轉換成數字
    if(!regex.IsMatch(args.Value))
    {
      args.IsValid=false;
    }
    else
    {
      //如果對2取模爲0就是偶數
      args.IsValid=(int.Parse(args.Value)%2==0);
    }
  }
}
複製代碼

 

  以下是什麼也不填寫時的驗證效果:

 asp.net夜話之九:驗證控件---(下)

  在上圖中,由於驗證奇偶性的CustomValidator驗證控件設置了ValidateEmptyText屬性爲true,所以即使所驗證的控件是空值時也會執行客戶端驗證。從這個例子中可以看出CustomValidator驗證控件可以不需要RequiredFieldValidator控件就能實現必填驗證,只要將它的ValidateEmptyText屬性設爲true即可。

  摘自:http://www.poluoluo.com/jzxy/200812/54518.html

發佈了11 篇原創文章 · 獲贊 11 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章