基于Ajax.net的验证

本文介绍了一个简单的AJAX应用实例,通过客户端与服务器端交互实现用户名的实时验证。具体包括服务器端逻辑处理、正则表达式匹配及客户端JavaScript回调函数。
       同第一个例子一样,前面的准备工作就不再陈述了。
       新建一个项目,命名为ajax,在WebForm1.aspx.cs文件的Page_Load()事件里注册:
1None.gifprivate void Page_Load(object sender, System.EventArgs e)
2ExpandedBlockStart.gifContractedBlock.gif        dot.gif{
3InBlock.gif            // 在此处放置用户代码以初始化页面
4InBlock.gif            AjaxPro.Utility.RegisterTypeForAjax(typeof(WebForm1));
5ExpandedBlockEnd.gif        }
然后开始服务器端的编程,写入如下的函数:
 1None.gifprivate bool IsusernameExit(string strusername)
 2ExpandedBlockStart.gifContractedBlock.gif        dot.gif{
 3InBlock.gif            bool bret=false;
 4InBlock.gif            switch (strusername.ToLower())
 5ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 6InBlock.gif                case "test":
 7InBlock.gif                case "tom":
 8InBlock.gif                case "jack":
 9InBlock.gif                case "ajax":
10InBlock.gif                case "aspnet":
11InBlock.gif                    bret=true;
12InBlock.gif                    break;
13ExpandedSubBlockEnd.gif            }

14InBlock.gif            return bret;
15ExpandedBlockEnd.gif        }

16None.gif        [AjaxPro.AjaxMethod]
17None.gif        public string GetReturnCode(string strusername)
18ExpandedBlockStart.gifContractedBlock.gif        dot.gif{
19InBlock.gif            if(!IsValidUsername(strusername))
20ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
21InBlock.gif                return "1";
22ExpandedSubBlockEnd.gif            }

23InBlock.gif            else if(!IsusernameExit(strusername))
24ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
25InBlock.gif                return "2";
26ExpandedSubBlockEnd.gif            }

27InBlock.gif            else
28ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
29InBlock.gif                return "0";
30ExpandedSubBlockEnd.gif            }

31InBlock.gif
32ExpandedBlockEnd.gif        }

33None.gif        private bool IsValidUsername(string strusername)
34ExpandedBlockStart.gifContractedBlock.gif        dot.gif{
35InBlock.gif            return (Regex.IsMatch(strusername,@"^(\w{3,15})$"));
36ExpandedBlockEnd.gif        }
这些是服务器端的函数,那么接下来就是客户端的编程,在WebForm1.aspx的HTML页面的<head></head>区域里
写入以下的函数:
 1None.gif<script language="javascript">
 2None.gif    function username()
 3ExpandedBlockStart.gifContractedBlock.gif    dot.gif{
 4InBlock.gif     var username=document.getElementById("tbUsername").value;
 5InBlock.gif    var a=ajax.WebForm1.GetReturnCode(username);
 6InBlock.gif    IsUsernameExit_callback(a);
 7ExpandedBlockEnd.gif    }

 8None.gif    function IsUsernameExit_callback(res)
 9ExpandedBlockStart.gifContractedBlock.gif    dot.gif{
10InBlock.gif     var msg=document.getElementById("ss");
11InBlock.gif     var bret=res.value;
12InBlock.gif     if(bret=="0")
13ExpandedSubBlockStart.gifContractedSubBlock.gif     dot.gif{
14InBlock.gif       msg.innerHTML="用户名存在";
15InBlock.gif       msg.style.color="green";
16ExpandedSubBlockEnd.gif     }

17InBlock.gif     else if(bret=="1")
18ExpandedSubBlockStart.gifContractedSubBlock.gif     dot.gif{
19InBlock.gif     msg.innerHTML="用户名长度必须在3到15之间,且不包含字母数字和下划线以外的字符!";
20InBlock.gif     msg.style.color="red";
21ExpandedSubBlockEnd.gif     }

22InBlock.gif     else
23ExpandedSubBlockStart.gifContractedSubBlock.gif     dot.gif{
24InBlock.gif     msg.innerHTML="用户名不存在";
25InBlock.gif     msg.style.color="red";
26ExpandedSubBlockEnd.gif     }

27ExpandedBlockEnd.gif    }

28None.gif        
29None.gif        </script>
恩,这样就差不多了,根据上面的代码,再在页面上拖几个相关的控件,就可以了,下面给出完整的代码
Webform1.aspx.cs
 1None.gifusing System;
 2None.gifusing System.Collections;
 3None.gifusing System.ComponentModel;
 4None.gifusing System.Data;
 5None.gifusing System.Drawing;
 6None.gifusing System.Web;
 7None.gifusing System.Web.SessionState;
 8None.gifusing System.Web.UI;
 9None.gifusing System.Web.UI.WebControls;
10None.gifusing System.Web.UI.HtmlControls;
11None.gifusing System.Text.RegularExpressions;
12None.gifnamespace ajax
13ExpandedBlockStart.gifContractedBlock.gifdot.gif{
14ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
15InBlock.gif    /// WebForm1 的摘要说明。
16ExpandedSubBlockEnd.gif    /// </summary>

17InBlock.gif    public class WebForm1 : System.Web.UI.Page
18ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
19InBlock.gif        protected System.Web.UI.WebControls.Label Label1;
20InBlock.gif        protected System.Web.UI.WebControls.TextBox tbUsername;
21InBlock.gif        protected System.Web.UI.WebControls.Label ss;
22InBlock.gif        protected System.Web.UI.WebControls.Label Label2;
23InBlock.gif    
24InBlock.gif        private void Page_Load(object sender, System.EventArgs e)
25ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
26InBlock.gif            // 在此处放置用户代码以初始化页面
27InBlock.gif            AjaxPro.Utility.RegisterTypeForAjax(typeof(WebForm1));
28ExpandedSubBlockEnd.gif        }

29InBlock.gif        
30InBlock.gif        private bool IsusernameExit(string strusername)
31ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
32InBlock.gif            bool bret=false;
33InBlock.gif            switch (strusername.ToLower())
34ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
35InBlock.gif                case "test":
36InBlock.gif                case "tom":
37InBlock.gif                case "jack":
38InBlock.gif                case "ajax":
39InBlock.gif                case "aspnet":
40InBlock.gif                    bret=true;
41InBlock.gif                    break;
42ExpandedSubBlockEnd.gif            }

43InBlock.gif            return bret;
44ExpandedSubBlockEnd.gif        }

45InBlock.gif        [AjaxPro.AjaxMethod]
46InBlock.gif        public string GetReturnCode(string strusername)
47ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
48InBlock.gif            if(!IsValidUsername(strusername))
49ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
50InBlock.gif                return "1";
51ExpandedSubBlockEnd.gif            }

52InBlock.gif            else if(!IsusernameExit(strusername))
53ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
54InBlock.gif                return "2";
55ExpandedSubBlockEnd.gif            }

56InBlock.gif            else
57ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
58InBlock.gif                return "0";
59ExpandedSubBlockEnd.gif            }

60InBlock.gif
61ExpandedSubBlockEnd.gif        }

62InBlock.gif        private bool IsValidUsername(string strusername)
63ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
64InBlock.gif            return (Regex.IsMatch(strusername,@"^(\w{3,15})$"));
65ExpandedSubBlockEnd.gif        }

66InBlock.gif
67ContractedSubBlock.gifExpandedSubBlockStart.gif        Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
68InBlock.gif        override protected void OnInit(EventArgs e)
69ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
70InBlock.gif            //
71InBlock.gif            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
72InBlock.gif            //
73InBlock.gif            InitializeComponent();
74InBlock.gif            base.OnInit(e);
75ExpandedSubBlockEnd.gif        }

76InBlock.gif        
77ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
78InBlock.gif        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
79InBlock.gif        /// 此方法的内容。
80ExpandedSubBlockEnd.gif        /// </summary>

81InBlock.gif        private void InitializeComponent()
82ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{  
83InBlock.gif            this.Load += new System.EventHandler(this.Page_Load);
84InBlock.gif
85ExpandedSubBlockEnd.gif        }

86ExpandedSubBlockEnd.gif        #endregion

87InBlock.gif
88InBlock.gif        
89InBlock.gif
90InBlock.gif    
91InBlock.gif        
92ExpandedSubBlockEnd.gif    }

93InBlock.gif        
94InBlock.gif    
95ExpandedBlockEnd.gif}

96None.gif
Webform1.aspx代码:
 1ExpandedBlockStart.gifContractedBlock.gif<%dot.gif@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="ajax.WebForm1" %>
 2None.gif<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
 3None.gif<HTML>
 4None.gif    <HEAD>
 5None.gif        <title>WebForm1</title>
 6None.gif        <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
 7None.gif        <meta content="C#" name="CODE_LANGUAGE">
 8None.gif        <meta content="JavaScript" name="vs_defaultClientScript">
 9None.gif        <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
10ExpandedBlockStart.gifContractedBlock.gif        <script language="javascript">dot.gif
11InBlock.gif    function username()
12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
13InBlock.gif     var username=document.getElementById("tbUsername").value;
14InBlock.gif    var a=ajax.WebForm1.GetReturnCode(username);
15InBlock.gif    IsUsernameExit_callback(a);
16ExpandedSubBlockEnd.gif    }

17InBlock.gif    function IsUsernameExit_callback(res)
18ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
19InBlock.gif     var msg=document.getElementById("ss");
20InBlock.gif     var bret=res.value;
21InBlock.gif     if(bret=="0")
22ExpandedSubBlockStart.gifContractedSubBlock.gif     dot.gif{
23InBlock.gif       msg.innerHTML="用户名存在";
24InBlock.gif       msg.style.color="green";
25ExpandedSubBlockEnd.gif     }

26InBlock.gif     else if(bret=="1")
27ExpandedSubBlockStart.gifContractedSubBlock.gif     dot.gif{
28InBlock.gif     msg.innerHTML="用户名长度必须在3到15之间,且不包含字母数字和下划线以外的字符!";
29InBlock.gif     msg.style.color="red";
30ExpandedSubBlockEnd.gif     }

31InBlock.gif     else
32ExpandedSubBlockStart.gifContractedSubBlock.gif     dot.gif{
33InBlock.gif     msg.innerHTML="用户名不存在";
34InBlock.gif     msg.style.color="red";
35ExpandedSubBlockEnd.gif     }

36ExpandedSubBlockEnd.gif    }

37InBlock.gif        
38ExpandedBlockEnd.gif        
</script>
39None.gif    
40None.gif
41None.gif    </HEAD>
42None.gif    <body>
43None.gif        <form id="Form1" method="post" runat="server">
44None.gif            &nbsp; <FONT face="宋体">
45None.gif                <asp:label id="Label1" style="Z-INDEX: 101; LEFT: 72px; POSITION: absolute; TOP: 72px" runat="server"
46None.gif                    Width="144px">给于ajax的数据验证</asp:label><asp:label id="Label2" style="Z-INDEX: 102; LEFT: 80px; POSITION: absolute; TOP: 128px" runat="server">用户名:</asp:label><asp:textbox id="tbUsername" onkeyup="username()" style="Z-INDEX: 103; LEFT: 160px; POSITION: absolute; TOP: 128px"
47None.gif                    runat="server"></asp:textbox><br>
48None.gif                <asp:Label id="ss" style="Z-INDEX: 104; LEFT: 80px; POSITION: absolute; TOP: 184px" runat="server"></asp:Label></FONT></form>
49None.gif    </body>
50None.gif</HTML>
51None.gif
 恩,这个例子没有涉及到数据库,当然了,只是个简单的引入。以后的工作量还是很大的。


转载于:https://www.cnblogs.com/symjie520/archive/2006/07/08/445777.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值