雖然大部分時間一直從事asp.net的開發,對於一些常用的asp.net服務器端驗證控件及它們的組合使用比較熟悉,如:
CompareValidator ——比較驗證控件
RangeValidator ——范圍驗證控件
RegularExpressionValidator ——正則驗證控件
RequiredFieldValidator ——必填驗證控件
但是一直沒去研究CustomValidator控件的用法,心中自然也有一種想法:好不容易從寫js驗證的痛苦中擺脫出來,如今是能不回去就不要回去了,但是有時候又會遇到一些用上面提到的驗證控件沒有辦法做到的情況(當然是指在客戶端沒辦法做到,我要是說在服務器端沒辦法做到就讓大家笑掉大牙了
),用自然想它在客戶端完成驗證了,至少也要先在客戶端驗證一下,通過客戶端驗證再到服務器端驗證,這樣減少數據的往返時間,降低網絡流量和保證反應及時性。
CustomValidator 是一個提供靈活驗證方式的控件,它也能在客戶端和服務器端驗證,分別提供了兩種驗證的方法原型:
服務器端驗證:
客戶端驗證(js):
無論對於客戶端驗證還是服務器端驗證,設置通過驗證的辦法就是將第二個參數的IsValid屬性設置為true即可,反之設置為false。
下面我以兩個例子來講述如何使用CustomValidator 來進行驗證,第一個用法是用來驗證TextBox,第二個用法是驗證CheckBoxList,確保其中只能選中一項,而且必須選中一項,對於TextBox這類的控件,我這里設置了對應的CustomValidator 的ValidateEmptyText="True"屬性,這樣即使控件值為空也能得到驗證。
全部代碼如下(這里是cs代碼和html代碼混合的模式):
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" %>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta name="keywords" content="CustomValidator控件用法" />
<meta name="description" content="周公講述CustomValidator控件用法" />
<title>CustomValidator控件用法</title>
</head>
<script language="javascript" type="text/javascript">
function ClientValidateUserName(source, args) {
//alert(source);source=CustomValidator1,為驗證控件
//alert(args);
var obj = document.getElementById("txtUserName");
if ((obj.value == "") || (obj.value.length > 10)) {
args.IsValid = false;
}
else {
args.IsValid = true;
}
}
function CheckCheckBoxList(source, args) {
var obj = document.getElementById('<%=this.cbAgeRangeList.ID%>'); //返回具有和屬性id的值相同或相似的對象集合
var k, right = false;
var length = (obj.all.tags('input').length); //返回obj對象里具有“input”標簽對象的集合
for (k = 0; k < length; k++) {
//alert(" name:"+obj.all.tags('input')[k].name+"是否:"+obj.all.tags('input')[k].checked);
if (obj.all.tags('input')[k].checked) // obj對象里具有input標簽對象鍵值為k的屬性為checked的值
{
right = !right;
}
}
if (right) {
args.IsValid = true;
}
else {
args.IsValid = false;
}
}
</script>
<script runat="server">
protected void Page_Load(Object Src, EventArgs E)
{
if (!IsPostBack) DataBind();
}
public void ServerValidateUserName(object source, ServerValidateEventArgs args)
{
if ((string.IsNullOrEmpty(txtUserName.Text)) || (txtUserName.Text.Length > 10))
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}
</script>
<body>
<form id="Form1" runat="server">
UserName:<asp:TextBox ID="txtUserName" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="錯誤信息" ControlToValidate="txtUserName"
ClientValidationFunction="ClientValidateUserName" ValidateEmptyText="True"></asp:CustomValidator>
<asp:Button ID="btnValid1" runat="server" Text="驗證用戶名" />
<asp:CheckBoxList ID="cbAgeRangeList" runat="server" Height="36px" Width="400px"
RepeatDirection="Vertical">
<asp:ListItem Value="1"><16</asp:ListItem>
<asp:ListItem Value="2">16-22</asp:ListItem>
<asp:ListItem Value="3">22-30</asp:ListItem>
<asp:ListItem Value="4">30-40</asp:ListItem>
<asp:ListItem Value="5">40-50</asp:ListItem>
<asp:ListItem Value="6">50-60</asp:ListItem>
<asp:ListItem Value="6">60-80</asp:ListItem>
<asp:ListItem Value="6">>80</asp:ListItem>
</asp:CheckBoxList>
<asp:CustomValidator ID="CustomValidator2" runat="server" ErrorMessage="請正確選擇年齡段"
ClientValidationFunction="CheckCheckBoxList"></asp:CustomValidator>
</form>
</body>
</html>
