1,客户端验证:
JavaScript 脚本,
代码
<script language="javascript">
//验证输入框的输入不能为空
function CheckForm(){
var ret=false;
if(document.all["txtName"].value=="")
document.all["name_erroe"].style.visibility="visible";
else if(document.all["txtPassword"].value=="")
document.all["password_error"].style.visibility="visible";
else
ret=true;
return ret;
}
</script>
<form id="form1" runat="server" onsubmit="return CheckForm()">
//验证输入框的输入不能为空
function CheckForm(){
var ret=false;
if(document.all["txtName"].value=="")
document.all["name_erroe"].style.visibility="visible";
else if(document.all["txtPassword"].value=="")
document.all["password_error"].style.visibility="visible";
else
ret=true;
return ret;
}
</script>
<form id="form1" runat="server" onsubmit="return CheckForm()">
仅仅是客户端验证,首先,客户端浏览器要支持脚本,但并非所有的浏览器都支持这种功能。其次,恶意用户可以很容易的破坏客户端脚本,从而提交不良数据
2,服务端验证
2.1 RequiredFieldValidator 控件
代码
<head runat="server">
<title>测试RequrieField</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please input Name"
ControlToValidate="txtName" InitialValue="" BackColor="Gray" Display="Dynamic">
</asp:RequiredFieldValidator>
<asp:Button ID="btnOk" runat="server" Text="确定" />
</div>
</form>
</body>
</html>
<title>测试RequrieField</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please input Name"
ControlToValidate="txtName" InitialValue="" BackColor="Gray" Display="Dynamic">
</asp:RequiredFieldValidator>
<asp:Button ID="btnOk" runat="server" Text="确定" />
</div>
</form>
</body>
</html>
2.2 CompareValidator控件
代码
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="年龄:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1"
runat="server" ErrorMessage="年龄必须大于0!" ControlToValidate ="TextBox1" Operator="GreaterThan" Type="Integer" ValueToCompare="0"></asp:CompareValidator>
</div>
<asp:Button ID="Button1" runat="server" Text="确定" />
</form>
</body>
</html>
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="年龄:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1"
runat="server" ErrorMessage="年龄必须大于0!" ControlToValidate ="TextBox1" Operator="GreaterThan" Type="Integer" ValueToCompare="0"></asp:CompareValidator>
</div>
<asp:Button ID="Button1" runat="server" Text="确定" />
</form>
</body>
</html>
2.3 RangeValidator 控件
代码
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lable" runat="server">百分数:</asp:Label>
<asp:TextBox ID="txtRange" runat="server"></asp:TextBox>
<asp:RangeValidator ID="RangeValidator1"
runat="server" ErrorMessage="百分数在:1-100之间!" ControlToValidate="txtRange" Type="Integer" MaximumValue="100" MinimumValue="1"></asp:RangeValidator>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lable" runat="server">百分数:</asp:Label>
<asp:TextBox ID="txtRange" runat="server"></asp:TextBox>
<asp:RangeValidator ID="RangeValidator1"
runat="server" ErrorMessage="百分数在:1-100之间!" ControlToValidate="txtRange" Type="Integer" MaximumValue="100" MinimumValue="1"></asp:RangeValidator>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>
2.4 RegularExpressionValidator 控件
代码
<asp:TextBox ID="txtIp" runat="server"></asp:TextBox>
<asp:Label ID="Label1" runat="server">数字输入:</asp:Label>
<asp:RegularExpressionValidator ID="regular" runat="server"
ErrorMessage="请输入数字!" ControlToValidate="txtIp" ValidationExpression="\d+"></asp:RegularExpressionValidator>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
<asp:Label ID="Label1" runat="server">数字输入:</asp:Label>
<asp:RegularExpressionValidator ID="regular" runat="server"
ErrorMessage="请输入数字!" ControlToValidate="txtIp" ValidationExpression="\d+"></asp:RegularExpressionValidator>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
2.5 CustomValidator
代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CustomValidateForm.aspx.cs" Inherits="TestCodeBehind.CustomValidateForm" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script language="javascript" type="text/javascript">
<!--
function CheckIsEven(source,args){
if(args.Value%2==0)
args.IsValid=true;
else
args.IsValid=false;
}
-->
</script>
<script language="C#" runat="server">
private void ValidIsEven(object source, ServerValidateEventArgs args)
{
int value=int.Parse(args.Value);
if (value % 2 == 0)
args.IsValid = true;
else
args.IsValid = false;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:Label ID="Label1" runat="server" Text="偶数:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CustomValidator ID="cutomer" runat="server"
ControlToValidate="TextBox1" ClientValidationFunction="CheckIsEven" OnServerValidate="ValidIsEven" ErrorMessage="请输入偶数!"></asp:CustomValidator>
<asp:Button ID="Button1" runat="server" Text="确定" />
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script language="javascript" type="text/javascript">
<!--
function CheckIsEven(source,args){
if(args.Value%2==0)
args.IsValid=true;
else
args.IsValid=false;
}
-->
</script>
<script language="C#" runat="server">
private void ValidIsEven(object source, ServerValidateEventArgs args)
{
int value=int.Parse(args.Value);
if (value % 2 == 0)
args.IsValid = true;
else
args.IsValid = false;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:Label ID="Label1" runat="server" Text="偶数:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CustomValidator ID="cutomer" runat="server"
ControlToValidate="TextBox1" ClientValidationFunction="CheckIsEven" OnServerValidate="ValidIsEven" ErrorMessage="请输入偶数!"></asp:CustomValidator>
<asp:Button ID="Button1" runat="server" Text="确定" />
</form>
</body>
</html>