首页 > 代码库 > 021-异步注册登录(检测用户名)

021-异步注册登录(检测用户名)

Register.html

 1 <!DOCTYPE html>
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5     <title></title>
 6     <script src="js/jquery-1.7.1.min.js"></script>
 7     <script>
 8         $(function () {
 9             //点击注册按钮,进行注册请求
10             $(#btnRegister).click(function () {
11                 //判断值不为空
12                 if ($(#uname).val() == ‘‘) {
13                     $(#uname).focus();
14                     return;
15                 }
16                 if ($(#upwd).val() == ‘‘) {
17                     $(#upwd).focus();
18                     return;
19                 }
20 
21                 //判断用户名是否存在
22                 if ($(#uname2).html() == X) {
23                     $(#uname).focus();
24                     return;
25                 }
26 
27                 //判断密码是否相同
28                 if ($(#upwd).val() != $(#upwd2).val()) {
29                     alert(两次密码不一致);
30                     $(#upwd2).val(‘‘);
31                     $(#upwd2).focus();
32                     return;
33                 }
34 
35                 $.post(
36                     Register.ashx,
37                     action=2& + $(#form1).serialize(),
38                     function (msg) {//msg:1表示成功,msg==0失败
39                         if (msg == 1) {
40                             location.href = "Login.html";
41                         } else {
42                             $(#msg).html(注册失败);
43                         }
44                     }
45                 );
46 
47             });
48 
49             //文本框失去焦点,判断用户名是否存在
50             $(#uname).blur(function () {
51                 $.post(
52                     Register.ashx,
53                     {
54                         action: 1,//用于进行用户名判断
55                         uname: $(#uname).val()
56                     },
57                     function (msg) {//1:存在,0:不存在
58                         if (msg == 1) {
59                             $(#uname2).html(X);
60                         } else {
61                             $(#uname2).html(‘‘);
62                         }
63                     }
64                 );
65             });
66         });
67     </script>
68 </head>
69 <body>
70     <form id="form1">
71         用户名:<input type="text" name="uname" id="uname" />
72         <span id="uname2"></span>
73         <br />
74         密码:<input type="password" name="upwd" id="upwd" />
75         <br />
76         重复密码:<input type="password" id="upwd2" />
77         <br />
78         <input type="button" id="btnRegister" value="注册" />
79         <div id="msg"></div>
80     </form>
81 </body>
82 </html>

Register.ashx

 1     public class Register : IHttpHandler
 2     {
 3 
 4         public void ProcessRequest(HttpContext context)
 5         {
 6             context.Response.ContentType = "text/plain";
 7 
 8             //操作类型:1判断用户名是否存在,2进行注册insert
 9             string action = context.Request["action"];
10             if (action.Equals("1"))
11             {
12                 string sql = "select count(*) from userinfo where username=@name";
13                 SqlParameter p = new SqlParameter("@name", context.Request["uname"]);
14 
15                 int obj = Convert.ToInt32(SqlHelper.ExecuteScalar(sql, p));
16                 context.Response.Write(obj);
17 
18             }
19             else if (action.Equals("2")) //注册
20             {
21                 string sql = "insert into userinfo values(@name,@pwd)";
22                 SqlParameter[] ps =
23                 {
24                     new SqlParameter("@name",context.Request["uname"]),
25                     new SqlParameter("@pwd",context.Request["upwd"])
26                 };
27 
28                 int result = SqlHelper.ExecuteNonQuery(sql, ps);
29                 context.Response.Write(result);
30             }
31 
32 
33         }
34 
35         public bool IsReusable
36         {
37             get
38             {
39                 return false;
40             }
41         }
42     }

Login.html

 1 <!DOCTYPE html>
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5     <title></title>
 6     <script src="js/jquery-1.7.1.min.js"></script>
 7     <script>
 8         $(function () {
 9             $(#btnLogin).click(function () {
10                 $.post(
11                     Login.ashx,
12                     $(#form1).serializeArray(),
13                     function (msg) {
14                         if (msg == 1) {//成功
15                             location.href = "Index.aspx";
16                         } else if (msg == 2) {//用户名错
17                             $(#msg).html(用户名错误);
18                         } else if (msg == 3) {
19                             $(#msg).html(密码错误);
20                         }
21                     }
22                 );
23             });
24         });
25     </script>
26 </head>
27 <body>
28     <form id="form1">
29         用户名:<input type="text" name="uname" />
30         <br />
31         密码:<input type="password" name="upwd" />
32         <br />
33         <input type="button" id="btnLogin" value="登录" />
34         <div id="msg"></div>
35     </form>
36 </body>
37 </html>

Login.ashx

 1     public class Login : IHttpHandler
 2     {
 3 
 4         public void ProcessRequest(HttpContext context)
 5         {
 6             context.Response.ContentType = "text/plain";
 7             int result = 1;//1成功,2用户名错,3密码错
 8 
 9             string uname = context.Request["uname"];
10             string upwd = context.Request["upwd"];
11 
12             string sql = "select userpwd from userinfo where username=@name";
13             SqlParameter p = new SqlParameter("@name", uname);
14 
15             object upwd2 = SqlHelper.ExecuteScalar(sql, p);
16             if (upwd2 == null)
17             {
18                 result = 2;
19             }
20             else if (upwd2.ToString().Equals(upwd))
21             {
22                 result = 1;
23             }
24             else
25             {
26                 result = 3;
27             }
28 
29             context.Response.Write(result);
30         }
31 
32         public bool IsReusable
33         {
34             get
35             {
36                 return false;
37             }
38         }
39     }

SqlHelper.cs

 1     public static class SqlHelper
 2     {
 3         private static string connStr =
 4             System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
 5 
 6         public static DataTable GetList(string sql, params SqlParameter[] ps)
 7         {
 8             using (SqlConnection conn = new SqlConnection(connStr))
 9             {
10                 SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
11                 sda.SelectCommand.Parameters.AddRange(ps);
12 
13                 DataTable dt = new DataTable();
14                 sda.Fill(dt);
15 
16                 return dt;
17             }
18         }
19 
20         public static int ExecuteNonQuery(string sql, params SqlParameter[] ps)
21         {
22             using (SqlConnection conn = new SqlConnection(connStr))
23             {
24                 SqlCommand cmd = new SqlCommand(sql, conn);
25                 cmd.Parameters.AddRange(ps);
26 
27                 conn.Open();
28                 return cmd.ExecuteNonQuery();
29             }
30         }
31 
32         public static object ExecuteScalar(string sql, params SqlParameter[] ps)
33         {
34             using (SqlConnection conn = new SqlConnection(connStr))
35             {
36                 SqlCommand cmd = new SqlCommand(sql, conn);
37                 cmd.Parameters.AddRange(ps);
38 
39                 conn.Open();
40                 return cmd.ExecuteScalar();
41             }
42         }
43 
44     }

Web.config

 1 <?xml version="1.0" encoding="utf-8"?>
 2 
 3 <!--
 4   有关如何配置 ASP.NET 应用程序的详细信息,请访问
 5   http://go.microsoft.com/fwlink/?LinkId=169433
 6   -->
 7 
 8 <configuration>
 9   <system.web>
10     <compilation debug="true" targetFramework="4.5" />
11     <httpRuntime targetFramework="4.5" />
12   </system.web>
13   <connectionStrings>
14     <add name="conn" connectionString="server=.;database=web1;uid=sa;pwd=123"/>
15   </connectionStrings>
16 </configuration>

 

021-异步注册登录(检测用户名)