验证控件的使用六(CustomValidator)

 
  
1 <% @ Page Language = " C# " %>
2
3 < script runat = " server " >
4
5 protected void Button1_Click(Object sender, EventArgs e) {
6 Label1.Text = " VALID NUMBER! " ;
7 }
8
9 </ script >
10
11 < html xmlns = " http://www.w3.org/1999/xhtml " >
12 < head id = " Head1 " runat = " server " >
13 < title > CustomValidator </ title >
14
15 < script type = " text/javascript " >
16 function validateNumber(oSrc, args) {
17 args.IsValid = (args.Value % 5 == 0 );
18 }
19 </ script >
20
21 </ head >
22 < body >
23 < form id = " form1 " runat = " server " >
24 < div >
25 < p >
26 Number:
27 < asp:TextBox ID = " TextBox1 "
28 Runat = " server " ></ asp:TextBox >
29 & nbsp;
30 < asp:CustomValidator ID = " CustomValidator1 " Runat = " server " ControlToValidate = " TextBox1 " Text = " Number must be divisible by 5 "
31 ClientValidationFunction = " validateNumber " >
32 </ asp:CustomValidator >
33 </ p >
34 < p >
35 < asp:Button ID = " Button1 " OnClick = " Button1_Click " Runat = " server " Text = " Button " ></ asp:Button >
36 </ p >
37 < p >
38 < asp:Label ID = " Label1 " Runat = " server " ></ asp:Label >
39 </ p >
40 </ div >
41 </ form >
42 </ body >
43 </ html >

 
  
1 <% @ Page Language = " C# " %>
2
3 < script runat = " server " >
4
5 protected void Button1_Click(Object sender, EventArgs e) {
6 if (Page.IsValid) {
7 Label1.Text = " VALID ENTRY! " ;
8 }
9 }
10
11 void ValidateNumber( object source, ServerValidateEventArgs args)
12 {
13 try
14 {
15 int num = int .Parse(args.Value);
16 args.IsValid = ((num % 5 ) == 0 );
17 }
18 catch (Exception ex)
19 {
20 args.IsValid = false ;
21 }
22 }
23
24 </ script >
25
26 < html xmlns = " http://www.w3.org/1999/xhtml " >
27 < head id = " Head1 " runat = " server " >
28 < title > CustomValidator </ title >
29 </ head >
30 < body >
31 < form id = " form1 " runat = " server " >
32 < div >
33 < p >
34 Number:
35 < asp:TextBox ID = " TextBox1 "
36 Runat = " server " ></ asp:TextBox >
37 & nbsp;
38 < asp:CustomValidator ID = " CustomValidator1 "
39 Runat = " server " ControlToValidate = " TextBox1 "
40 Text = " Number must be divisible by 5 "
41 OnServerValidate = " ValidateNumber " ></ asp:CustomValidator >
42 </ p >
43 < p >
44 < asp:Button ID = " Button1 " OnClick = " Button1_Click "
45 Runat = " server " Text = " Button " ></ asp:Button >
46 </ p >
47 < p >
48 < asp:Label ID = " Label1 " Runat = " server " ></ asp:Label >
49 </ p >
50 </ div >
51 </ form >
52 </ body >
53 </ html >

 
  
1 < asp:CustomValidator ID = " CustomValidator1 "
2 Runat = " server " ControlToValidate = " TextBox1 "
3 Text = " Number must be divisible by 5 "
4 ClientValidationFunction = " validateNumber "
5 OnServerValidate = " ValidateNumber " ></ asp:CustomValidator >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值