标签:style blog http io color ar os 使用 java
CustomValidator 类:对输入控件执行用户定义的验证。
ClientValidationFunction 获取或设置用于验证的自定义客户端脚本函数的名称。
ControlToValidate 获取或设置要验证的输入控件。 (继承自 BaseValidator。)
ErrorMessage 获取或设置验证失败时 ValidationSummary 控件中显示的错误消息的文本。 (继承自 BaseValidator。)
IsValid 获取或设置一个值,该值指示关联的输入控件是否通过验证。 (继承自 BaseValidator。)
OnServerValidate 为 CustomValidator 控件引发 ServerValidate 事件。
该检验类有一个ClientValidationFunction表示可以使用用户自定义的客户端脚本函数,采用以下方式来进行定义
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>
客户端检验:
<%@ 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>
标签:style blog http io color ar os 使用 java
原文地址:http://www.cnblogs.com/tianyake/p/4071828.html