[轉].net自訂驗證控制項CustomValidator的使用

來源:互聯網
上載者:User

標籤:class   code   http   java   tar   ext   

本文轉自:http://tech.cncms.com/web/aspnet/96310.html

 

CustomValidator驗證控制項,可以自訂驗證函式,實現其它幾個驗證控制項不能實現的驗證規則,最簡單的應用如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CustomValidatorDemo.aspx.cs" Inherits="CustomValidatorDemo" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title>自訂驗證控制項的使用</title>    <script type="text/javascript">     function IsEven(source, args)    {        if (args.Value % 2 == 0 )        {            arg.IsValid = true;        }         else         {            args.IsValid = false;        }    }    </script></head><body>    <form id="form1" runat="server">    <div>        偶數:<asp:TextBox ID="txtEven" runat="server"></asp:TextBox>        <asp:CustomValidator ID="valgEven" runat="server" ControlToValidate="txtEven" ErrorMessage="所輸資料不是偶數"            ClientValidationFunction="IsEven">*</asp:CustomValidator>        <br />        <asp:ValidationSummary ID="valsErrorMessage" runat="server" />        <asp:Button ID="btnSubmit" runat="server" Text="提交" />    </div>    </form></body></html>

1、函數名由 ClientValidationFunction="IsEven"指定,函數形參名字可以自己決定,比方有些執行個體把args寫成arguments,也是可以的;

2、形參args主要有兩個屬性args.Value和arg.IsValid,這兩個屬性要注意大小寫

3、自訂驗證控制項可觸發onservervalidate事件,在伺服器端也可以編寫驗證代碼,但,如果這樣做,就有點失去了驗證控制項在用戶端實現驗證的意義了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.