Ajax检测注册用户是否存在
生活随笔
收集整理的這篇文章主要介紹了
Ajax检测注册用户是否存在
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
HTML代碼如下:
LoginValidate.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LoginValidate.aspx.cs" Inherits="LoginValidate" %>
<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= "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)
? ?? ?? ?? ?{
? ?? ?? ?? ?? ? document.getElementById("Msg").innerHTML=xmlHttp.responseText;
? ?? ?? ?? ?}
? ?? ???}
? ? }
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width: 487px">
? ? <tr>
? ?? ???<td style="width: 70px">
? ?? ?? ?? ?用戶名:</td>
? ?? ???<td style="width: 231px"><input id="username" type="text" />
? ?? ???<input id="Button1" type="button" value="button" οnclick="CheckUserName();" /></td>
? ?? ???<td id="Msg"></td>
? ? </tr>
? ? <tr>
? ?? ???<td style="width: 70px">
? ?? ???</td>
? ?? ???<td style="width: 231px">
? ?? ???</td>
? ?? ???<td>
? ?? ?? ?? ?</td>
? ? </tr>
</table>
</div>
</form>
</body>
</html>
服務器端代碼如下:(這里我是用的臨時處理文件.ashx)
LoginValidate.ashx
<%@ WebHandler Language="C#" Class="LoginValidate" %>
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
public class LoginValidate : IHttpHandler
{
? ?
? ? public void ProcessRequest (HttpContext context)
? ? {
? ?? ???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("該用戶已經(jīng)有人使用!");
? ?? ???}
? ?? ???else
? ?? ???{
? ?? ?? ?? ?context.Response.Write("恭喜你!"+username+"可以使用!");
? ?? ???}
? ?? ???System.Threading.Thread.Sleep(3000);
? ? }
? ? //數(shù)據(jù)庫連接字符串
? ? public static string strCon = "Data Source=.;database=exam;uid=sa;pwd=;";
? ? /// <summary>
? ? /// 執(zhí)行SQL語句,返回DataSet
? ? /// </summary>
? ? /// <param name="strSQL"></param>
? ? /// <returns></returns>
? ? public??DataSet ReDataSet(string strSQL)
? ? {
? ?? ???SqlConnection con = new SqlConnection(strCon);
? ?? ???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;
? ?? ???}
? ? }
}
LoginValidate.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LoginValidate.aspx.cs" Inherits="LoginValidate" %>
<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= "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)
? ?? ?? ?? ?{
? ?? ?? ?? ?? ? document.getElementById("Msg").innerHTML=xmlHttp.responseText;
? ?? ?? ?? ?}
? ?? ???}
? ? }
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width: 487px">
? ? <tr>
? ?? ???<td style="width: 70px">
? ?? ?? ?? ?用戶名:</td>
? ?? ???<td style="width: 231px"><input id="username" type="text" />
? ?? ???<input id="Button1" type="button" value="button" οnclick="CheckUserName();" /></td>
? ?? ???<td id="Msg"></td>
? ? </tr>
? ? <tr>
? ?? ???<td style="width: 70px">
? ?? ???</td>
? ?? ???<td style="width: 231px">
? ?? ???</td>
? ?? ???<td>
? ?? ?? ?? ?</td>
? ? </tr>
</table>
</div>
</form>
</body>
</html>
服務器端代碼如下:(這里我是用的臨時處理文件.ashx)
LoginValidate.ashx
<%@ WebHandler Language="C#" Class="LoginValidate" %>
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
public class LoginValidate : IHttpHandler
{
? ?
? ? public void ProcessRequest (HttpContext context)
? ? {
? ?? ???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("該用戶已經(jīng)有人使用!");
? ?? ???}
? ?? ???else
? ?? ???{
? ?? ?? ?? ?context.Response.Write("恭喜你!"+username+"可以使用!");
? ?? ???}
? ?? ???System.Threading.Thread.Sleep(3000);
? ? }
? ? //數(shù)據(jù)庫連接字符串
? ? public static string strCon = "Data Source=.;database=exam;uid=sa;pwd=;";
? ? /// <summary>
? ? /// 執(zhí)行SQL語句,返回DataSet
? ? /// </summary>
? ? /// <param name="strSQL"></param>
? ? /// <returns></returns>
? ? public??DataSet ReDataSet(string strSQL)
? ? {
? ?? ???SqlConnection con = new SqlConnection(strCon);
? ?? ???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;
? ?? ???}
? ? }
}
轉載于:https://www.cnblogs.com/beniao/archive/2008/03/20/1114020.html
總結
以上是生活随笔為你收集整理的Ajax检测注册用户是否存在的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 你印象中的高仓健在哪一部电影?
- 下一篇: Oracle常用傻瓜问题1000问