zoukankan      html  css  js  c++  java
  • 实现用户注册,要求输入用户名之后,焦点离开判断用户名是否存在,如果存在给出提示,不存在允许注册改用户。要求使用AJAX

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    
    namespace AJAX
    {
        /// <summary>
        /// LoginHandler 的摘要说明
        /// </summary>
        public class LoginHandler : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
    
                string UserName = context.Request["UserName"];
    
                string UserPwd = context.Request["UserPwd"];
                if (string.IsNullOrEmpty(UserName))
                {
                    context.Response.Redirect("Login.html");
                }
                string sql = "select count(*) from Users where UserName=@u ";
                SqlParameter[] p = { new SqlParameter("@u",UserName)};
                int i = Convert.ToInt32(SQLHelper.ExecuteScalar(sql,p));
                if (i==1)
                {
                    context.Response.Write("1");
                }
                else
                {
                    string s = "insert into Users (UserName,Pwd) values (@UserName,@p)";
                    SqlParameter[] sp = { new SqlParameter("@UserName",UserName),
                                          new SqlParameter("@p",UserPwd)};
                    int j = Convert.ToInt32(SQLHelper.NonExQuery(s, sp));
                    if (j == 1)
                    {
                        context.Response.Write("0");
                    }
                }
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }
    <!DOCTYPE html>
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
        <meta charset="utf-8" />
        <script src="js/jquery-3.3.1.js"></script>    
        <script>
            $(function () {
                $("#UserName").blur(function () {
                    var UserName = $("#UserName").val();                
                    $.post("LoginHandler.ashx", { "UserName": UserName},function (data) {
                        if (data == "1") {
                            $('#btn').attr('disabled',"true");
                            alert("已经注册过了");
                        }
                        else {
                            $('#btn').removeAttr("disabled");
                        }
                    });
                });
                $("#btn").click(function () {
                    var UserName = $("#UserName").val();
                    var UserPwd = $("#UserPwd").val();
                    $.post("LoginHandler.ashx", {"UserName": UserName, "UserPwd": UserPwd }, function (data) {
                        if (data == "0") {
                            alert("注册成功!");
                        }
                    })
                })
            });
        </script>
    </head>
    <body>
        用户名:<input type="text" name="UserName" id="UserName" />
        <br />&nbsp;&nbsp;码:<input type="password" name="UserPwd" id="UserPwd" />
        <br />
        <input type="button" value="注册" id="btn" />
    </body>
    </html>
  • 相关阅读:
    替换Srun3000解决掉网问题
    了解Equals所发生的事情
    WPF自定义TextBox及ScrollViewer
    自制多类编码转换工具
    .NET4.5之自制多LRC转SRT小工具
    JAVA_list总结
    记一次线程Timer导致的线程安全问题修正
    JAVA_匿名内部类
    JAVA_map总结
    JAVA_闭包
  • 原文地址:https://www.cnblogs.com/zhang1997/p/8759983.html
Copyright © 2011-2022 走看看