ajax验证用户名是否存在

LoginValidate.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LoginValidate.aspx.cs" Inherits="Admin_LoginValidate" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>验证用户名是否存在</title>

    <script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest()
{
       if(window.ActiveXObject) 
       {
         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
       }
       else if(window.XMLHttpRequest)
       {
         xmlHttp = new XMLHttpRequest();
       }
}
//处理方法
function CheckUserName()
{
       createXMLHttpRequest();
       var url= "../ashx/LoginValidate.ashx?username="+document.getElementById("username").value;
       xmlHttp.open("GET",url,true);
       xmlHttp.onreadystatechange=ShowResult;
       xmlHttp.send(null);
     //  document.getElementById("Msg").innerHTML='';
     
}
//回调方法
function ShowResult()
{
       if(xmlHttp.readyState==4)
       {
         if (xmlHttp.status == 200)
         {
            alert("Server is done!");
           alert(xmlHttp.responseText);
           document.getElementById("Msg").innerHTML=xmlHttp.responseText;
         }
         else if (xmlHttp.status == 404)
        {
         alert("Request URL does not exist");
        }
         else if (xmlHttp.status == 403)
         {
         alert("Access   or  sql   denied.");
         }
         else
         {
         alert("Error: status code is " + xmlHttp.status);
         }
        
        
       }
   
}
    </script>

</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h1 align="center">
                验证用户名是否存在</h1>
            <table align="center" style="width: 487px">
                <tr>
                    <td style="width: 70px">
                        用户名:</td>
                    <td style="width: 231px">
                        <input id="username" οnchange="CheckUserName();" type="text" />
                        <input id="Button1" type="button" value="button" οnclick="CheckUserName();" /></td>
                    <td id="Msg1">
                        <asp:Label ID="Msg" runat="server" ForeColor="Blue"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td style="width: 70px">
                    </td>
                    <td style="width: 231px">
                    </td>
                    <td>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

LoginValidate.ashx

<%@ WebHandler Language="C#" Class="LoginValidate" %>

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public class LoginValidate : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
        //context.Response.ContentType = "text/plain";
        //context.Response.Write("Hello World");
        context.Response.ContentType = "text/plain";
        string username = context.Request.QueryString["username"].ToString();
        string strSQL = "select username from users where username='" + username + "'";
        if (ReDataSet(strSQL).Tables[0].Rows.Count > 0)
        {
            context.Response.Write("该用户已经有人使用!");
        }
        else
        {
          if (username.Trim().Length>int.Parse("0"))
            {
            context.Response.Write("恭喜你!" + username + "可以使用!");
            }
            else
            {
                context.Response.Write("请您输入用户名!");  
            }

        }
        System.Threading.Thread.Sleep(1000);

    }

    //数据库连接字符串
    public static string strCon = "Data Source=.;database=test;uid=sa;pwd=;";

    /// <summary>
    /// 执行SQL语句,返回DataSet
    /// </summary>
    /// <param name="strSQL"></param>
    /// <returns></returns>
    public DataSet ReDataSet(string strSQL)
    {
      //  SqlConnection con = new SqlConnection(strCon);
        using (SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]))
        try
        {
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(strSQL, con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            con.Close();
        }
    }

    /// <summary>
    /// 不重复调用
    /// </summary>
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值