CustomValidator控件使用

验证要求:1 对一个输入进行多方面的验证 2 对checkboxlist进行至少选择一项的验证

1 对一个输入进行多方面的验证(采用客户端验证)

举例代码:

ContractedBlock.gif ExpandedBlockStart.gif
 
   
<% @ Page Language = " C# " AutoEventWireup = " true " CodeFile = " Default4.aspx.cs " Inherits = " Default4 " %>
<! 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 runat ="server" >
< title > 无标题页 </ title >
< script src ="simpletree/jquery-1.4.2.js" type ="text/javascript" ></ script >
< script language ="javascript" type ="text/javascript" >
// 验证输入
function ValidateInput(source,args)
{
args.IsValid
= true ;

if (args.Value.length == 0 )
{
$(
" # " + source.id).html( " 不能为空 " );
args.IsValid
= false ;
}
else if ( ! args.Value.match( / ^[0-9]*[1-9][0-9]*$ / ))
{
$(
" # " + source.id).html( " 必须为正整数 " );
args.IsValid
= false ;
}
else
{
if (args.Value < 1 || args.Value > 365 )
{
args.IsValid
= false ;
$(
" # " + source.id).html( " 必须在1到365之间 " );
}
}
}
</ script >
</ head >
< body >
< form id ="form1" runat ="server" >
< asp:TextBox ID ="txtMinSpace" runat ="server" ></ asp:TextBox >
< asp:CustomValidator ID ="dfdf" runat ="server"
ControlToValidate
="txtMinSpace"
SetFocusOnError
="True"
ClientValidationFunction
="ValidateInput"
ErrorMessage
="Requi"
ValidateEmptyText
="true"
Display
="Dynamic" >
</ asp:CustomValidator >
< br />
< asp:Button ID ="test" runat ="server" Text ="提交" onclick ="test_Click" />
</ form >
</ body >
</ html >

 注意事项:ValidateEmptyText需要验证空值时需要设置此属性,否则不启作用。

 

2 对checkboxlist进行至少选择一项的验证(采用客户端验证)

举例代码:

ContractedBlock.gif ExpandedBlockStart.gif
 
   
<% @ Page Language = " C# " AutoEventWireup = " true " CodeFile = " Default4.aspx.cs " Inherits = " Default4 " %>
<! 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 runat ="server" >
< title > 无标题页 </ title >
< script src ="simpletree/jquery-1.4.2.js" type ="text/javascript" ></ script >
< script language ="javascript" type ="text/javascript" >
// 验证选择
function ValidateCheckList(source,args)
{
args.IsValid
= true ;

if ($( " #<%=cblAction.ClientID%> input[@type=checkbox]:checked " ).size() == 0 )
args.IsValid
= false ;
}
</ script >
</ head >
< body >
< form id ="form1" runat ="server" >

< asp:CheckBoxList ID ="cblAction" runat ="server" >
< asp:ListItem Value ="1" Selected ="True" > 选项1 </ asp:ListItem >
< asp:ListItem Value ="2" Selected ="True" > 选项1 </ asp:ListItem >
< asp:ListItem Value ="3" Selected ="True" > 选项1 </ asp:ListItem >
</ asp:CheckBoxList >
< asp:CustomValidator ID ="ctvAction" runat ="server"
ErrorMessage
="至少选择一个" ClientValidationFunction ="ValidateCheckList" Display ="Dynamic" >
</ asp:CustomValidator >
< br />
< asp:Button ID ="test" runat ="server" Text ="提交" onclick ="test_Click" />
</ form >
</ body >
</ html >

 

 

 

 

转载于:https://www.cnblogs.com/neekerss/archive/2010/09/09/1822274.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值