CustomValidator,客户端,服务器端验证
Code
<%@ Page Language="C#" %>
<script runat="server">
protected void Button1_Click(Object sender, EventArgs e)
{
if (Page.IsValid)
{
Label1.Text = "输入的是5的倍数,args.IsValid=true,CustomValidator验证通过!";
}
}
//服务器端验证必须是5的倍数,如果有客户端验证则先执行客户端的验证
void ValidateNumber(object source, ServerValidateEventArgs args)
{
try
{
int num = int.Parse(args.Value);
args.IsValid = ((num % 5) == 0);
}
catch (Exception ex)
{
args.IsValid = false;
}
}
//服务器端验证必须是大于8,如果有客户端验证则先执行客户端的验证
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
if ((args.Value.Length >= 8))//args.Value获得了客户端的数据
args.IsValid = true;
else
args.IsValid = false;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>CustomValidator</title>
<script language="JavaScript">
function validateNumber(oSrc, args)
{
args.IsValid = (args.Value % 5 == 0);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
Number:<br />
<asp:TextBox ID="TextBox1"
Runat="server"></asp:TextBox>
<br />
<br />
<asp:CustomValidator ID="CustomValidator1"
Runat="server" ControlToValidate="TextBox1"
ErrorMessage="服务器端验证:必须是5倍数"
OnServerValidate="ValidateNumber">
</asp:CustomValidator><br />
<asp:CustomValidator ID="CustomValidator2"
Runat="server" ControlToValidate="TextBox1"
ErrorMessage="客户端验证:必须是5倍数"
ClientValidationFunction="validateNumber">
</asp:CustomValidator><br />
<asp:CustomValidator ID="CustomValidator3" runat="server"
ControlToValidate="TextBox1"
ErrorMessage="至少8位字符串"
OnServerValidate="CustomValidator1_ServerValidate">
</asp:CustomValidator>
<br />
<br />
<asp:Button ID="Button1" OnClick="Button1_Click"
Runat="server" Text="验证"></asp:Button>
<br />
<br />
<asp:Label ID="Label1" Runat="server"></asp:Label>
<br />
<br />
<br />
在没有输入任何内容的情况下,验证控件失效</div>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<script runat="server">
protected void Button1_Click(Object sender, EventArgs e)
{
if (Page.IsValid)
{
Label1.Text = "输入的是5的倍数,args.IsValid=true,CustomValidator验证通过!";
}
}
//服务器端验证必须是5的倍数,如果有客户端验证则先执行客户端的验证
void ValidateNumber(object source, ServerValidateEventArgs args)
{
try
{
int num = int.Parse(args.Value);
args.IsValid = ((num % 5) == 0);
}
catch (Exception ex)
{
args.IsValid = false;
}
}
//服务器端验证必须是大于8,如果有客户端验证则先执行客户端的验证
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
if ((args.Value.Length >= 8))//args.Value获得了客户端的数据
args.IsValid = true;
else
args.IsValid = false;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>CustomValidator</title>
<script language="JavaScript">
function validateNumber(oSrc, args)
{
args.IsValid = (args.Value % 5 == 0);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
Number:<br />
<asp:TextBox ID="TextBox1"
Runat="server"></asp:TextBox>
<br />
<br />
<asp:CustomValidator ID="CustomValidator1"
Runat="server" ControlToValidate="TextBox1"
ErrorMessage="服务器端验证:必须是5倍数"
OnServerValidate="ValidateNumber">
</asp:CustomValidator><br />
<asp:CustomValidator ID="CustomValidator2"
Runat="server" ControlToValidate="TextBox1"
ErrorMessage="客户端验证:必须是5倍数"
ClientValidationFunction="validateNumber">
</asp:CustomValidator><br />
<asp:CustomValidator ID="CustomValidator3" runat="server"
ControlToValidate="TextBox1"
ErrorMessage="至少8位字符串"
OnServerValidate="CustomValidator1_ServerValidate">
</asp:CustomValidator>
<br />
<br />
<asp:Button ID="Button1" OnClick="Button1_Click"
Runat="server" Text="验证"></asp:Button>
<br />
<br />
<asp:Label ID="Label1" Runat="server"></asp:Label>
<br />
<br />
<br />
在没有输入任何内容的情况下,验证控件失效</div>
</form>
</body>
</html>