CustomValidator 類:對輸入控件執行用戶定義的驗證。javascript
ClientValidationFunction 獲取或設置用於驗證的自定義客戶端腳本函數的名稱。html
ControlToValidate 獲取或設置要驗證的輸入控件。 (繼承自 BaseValidator。)java
ErrorMessage 獲取或設置驗證失敗時 ValidationSummary 控件中顯示的錯誤消息的文本。 (繼承自 BaseValidator。)web
IsValid 獲取或設置一個值,該值指示關聯的輸入控件是否經過驗證。 (繼承自 BaseValidator。)函數
OnServerValidate 爲 CustomValidator 控件引起 ServerValidate 事件。ui
該檢驗類有一個ClientValidationFunction表示能夠使用用戶自定義的客戶端腳本函數,採用如下方式來進行定義spa
function ValidationFunctionName(source, arguments)
arguments參數是一個具備如下兩個屬性的對象:Value 和 IsValid。 使用此參數能夠獲取控件的值,以根據自定義驗證例程驗證並指示該值是否有效。
ServerValidate一樣須要須要定義相應的事件,定義的格式以下
void ServerValidation (object source, ServerValidateEventArgs args) { args.IsValid = (CheckBox1.Checked == true); }
服務端校驗
<%@ Page Language="C#" AutoEventWireup="True" %> <!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> <title>CustomValidator ServerValidate Example</title> <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-Names="Verdana" Font-Size="10pt" runat="server" AssociatedControlID="Text1"/> <br /> <asp:TextBox id="Text1" runat="server" /> <asp:CustomValidator id="CustomValidator1" ControlToValidate="Text1" Display="Static" ErrorMessage="Not an even number!" ForeColor="green" Font-Names="verdana" Font-Size="10pt" OnServerValidate="ServerValidation" runat="server"/> <br /> <asp:Button id="Button1" Text="Validate" OnClick="ValidateBtn_OnClick" runat="server"/> </form> </body> </html>
客戶端檢驗:code
<%@ 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>