佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > Ajax實現(xiàn)異步刷新驗證用戶名是否已存在的具體方法

Ajax實現(xiàn)異步刷新驗證用戶名是否已存在的具體方法

熱門標簽:客戶服務 百度AI接口 國美全國運營中心 人工智能 網(wǎng)站排名優(yōu)化 電商新玩法 科大訊飛語音識別系統(tǒng) 電銷業(yè)務

都是簡單的實例,所以直接發(fā)代碼

靜態(tài)頁面Ajax.html

復制代碼 代碼如下:

html>
    head>
        title>Ajax/title>
        script type="text/javascript">
            function loadXMLDoc() {
                if (document.getElementById("account").value == "") {
                    document.getElementById("accDiv").innerHTML = "用戶名不能為空";
                    return;
                }
                var xmlHttp;
                if(window.XMLHttpRequest) { // code for IE7+
                    xmlHttp = new XMLHttpRequest();
                }
                else { // code for IE5/IE6
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                }

                xmlHttp.onreadystatechange = function () {
                    if (xmlHttp.readyState == 4 xmlHttp.status == 200) {
                        //document.getElementById("myDiv").innerHTML=xmlHttp.responseText;
                        if (xmlHttp.responseText == "true") {
                            document.getElementById("accDiv").innerHTML = "用戶名不可用";
                        }
                        else {
                            document.getElementById("accDiv").innerHTML = "用戶名可用";
                        }
                    }
                }
                var a = document.getElementById("account").value;
                // get
                xmlHttp.open("GET", "validate.aspx?account=" + a + "random=" + Math.random, true);
                xmlHttp.send();
            }
            function delData() {
                document.getElementById("account").value = "";
                document.getElementById("accDiv").innerHTML = "";
            }
        /script>
    /head>
    body>
        h3>ajax/h3>
        table>
            tr>
                td>賬號:/td>td>input id="account" type="text" onblur="loadXMLDoc();" onfocus="delData();"/>/td>td>div id="accDiv">/div>/td>
            /tr>
            tr>
                td>密碼:/td>td>input id="passwd" type="password" />/td>
            /tr>
            tr>
                td>確認密碼:/td>td>input id="vPasswd" type="password" />/td>
            /tr>
            tr>
                td>姓名:/td>td>input id="name" type="text" />/td>
            /tr>
        /table>

    /body>
/html>


在賬號輸入框失去焦點時調用函數(shù)

訪問服務器使用的是Get方法,所以在參數(shù)處使用了附加隨機碼來避免緩存。

驗證頁面validate.aspx后臺代碼:

復制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.Sql;
using System.Data.SqlClient;

public partial class Ajax_validate_validate : System.Web.UI.Page
{
    public SqlConnection conn;

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Clear();
        if (Exists(Request.QueryString["account"]))
            Response.Write("true");
        else
            Response.Write("false");
        Response.End();
    }
    /// summary>
    /// 獲取數(shù)據(jù)庫連接
    /// /summary>
    /// returns>/returns>
    protected SqlConnection GetConnection()
    {
        string str = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        conn = new SqlConnection(str);
        return conn;
    }
    protected bool Exists(string account)
    {
        using (GetConnection())
        {
            try
            {
                conn.Open();
                string sqlStr = "select count(*) from userinfo where account='" + account + "'";
                SqlCommand cmd = new SqlCommand(sqlStr, conn);
                int row = Convert.ToInt32(cmd.ExecuteScalar());
                if (row > 0)
                    return true;
                else
                    return false;
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                conn.Close();
            }
        }
    }
}

在后臺中驗證用戶名是否已經(jīng)存在于數(shù)據(jù)庫中,返回真或者假

運行結果:

數(shù)據(jù)庫很簡單,只建了一張表userinfo,有3個字段:account、passwd、name

注意:在后臺往請求頁面寫數(shù)據(jù)時,當寫完要發(fā)送的數(shù)據(jù)之后,需要調用Response.end()方法來終止寫入,否則可能會發(fā)送一個完整頁面過去。

您可能感興趣的文章:
  • jquery ajax 檢測用戶注冊時用戶名是否存在
  • Ajax實時驗證用戶名/郵箱等是否已經(jīng)存在的代碼打包
  • PHP+Ajax異步通訊實現(xiàn)用戶名郵箱驗證是否已注冊( 2種方法實現(xiàn))
  • 基于jQuery實現(xiàn)的Ajax 驗證用戶名是否存在的實現(xiàn)代碼
  • jsp+ajax實現(xiàn)無刷新(鼠標離開文本框即驗證用戶名)實現(xiàn)思路
  • PHP+AJAX實現(xiàn)無刷新注冊(帶用戶名實時檢測)
  • ajax 檢測用戶名是否被占用
  • Asp.net下利用Jquery Ajax實現(xiàn)用戶注冊檢測(驗證用戶名是否存)
  • PHP+Ajax檢測用戶名或郵件注冊時是否已經(jīng)存在實例教程
  • Ajax異步檢查用戶名是否存在

標簽:棗莊 廈門 攀枝花 益陽 POS機 咸寧 南平 拉薩

巨人網(wǎng)絡通訊聲明:本文標題《Ajax實現(xiàn)異步刷新驗證用戶名是否已存在的具體方法》,本文關鍵詞  ;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    宜章县| 安化县| 安泽县| 泰安市| 菏泽市| 唐河县| 兴安县| 灯塔市| 古浪县| 鲁甸县| 呼和浩特市| 出国| 平武县| 陵川县| 枣庄市| 自治县| 含山县| 巴南区| 乌鲁木齐市| 岑巩县| 岗巴县| 新余市| 钦州市| 合山市| 桂阳县| 宜黄县| 九江县| 开原市| 同德县| 双桥区| 洮南市| 耿马| 济源市| 湖南省| 宁河县| 濮阳市| 屏东县| 颍上县| 马关县| 大埔区| 黎城县|