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>
  • 相关阅读:
    Android轻量级的开源缓存框架ASimpleCache
    ESP8266学习笔记6:ESP8266规范wifi连接操作
    javascript——正則表達式
    STL经常使用遍历算法for_each和transform的比較
    OpenGL(八)使用 subroutine 切换可编程管线
    (一二〇)CALayer的一些特性
    Android 5.0 怎样正确启用isLoggable(一)__使用具体解释
    Elasticsearch的javaAPI之query dsl-queries
    kettle使用log4j管理输出日志
    YY博客园UML用例图-活动图-状态图之博客模块
  • 原文地址:https://www.cnblogs.com/zhang1997/p/8759983.html
Copyright © 2011-2022 走看看