午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

    Ajax實(shí)現(xiàn)異步刷新驗(yàn)證用戶名是否已存在的具體方法
    來(lái)源:易賢網(wǎng) 閱讀:2475 次 日期:2014-10-11 14:18:17
    溫馨提示:易賢網(wǎng)小編為您整理了“Ajax實(shí)現(xiàn)異步刷新驗(yàn)證用戶名是否已存在的具體方法”,方便廣大網(wǎng)友查閱!

    由于要做一個(gè)注冊(cè)頁(yè)面,看到許多網(wǎng)站上都是使用Ajax異步刷新驗(yàn)證用戶名是否可用的,所以自己也動(dòng)手做一個(gè)小實(shí)例

    都是簡(jiǎn)單的實(shí)例,所以直接發(fā)代碼

    靜態(tài)頁(yè)面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>賬號(hào):</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>確認(rèn)密碼:</td><td><input id="vPasswd" type="password" /></td>

    </tr>

    <tr>

    <td>姓名:</td><td><input id="name" type="text" /></td>

    </tr>

    </table>

    </body>

    </html>

    在賬號(hào)輸入框失去焦點(diǎn)時(shí)調(diào)用函數(shù)

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

    驗(yàn)證頁(yè)面validate.aspx后臺(tái)代碼:

    代碼如下:

    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ù)庫(kù)連接

    /// </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();

    }

    }

    }

    }

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

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

    注意:在后臺(tái)往請(qǐng)求頁(yè)面寫數(shù)據(jù)時(shí),當(dāng)寫完要發(fā)送的數(shù)據(jù)之后,需要調(diào)用Response.end()方法來(lái)終止寫入,否則可能會(huì)發(fā)送一個(gè)完整頁(yè)面過(guò)去。

    更多信息請(qǐng)查看IT技術(shù)專欄

    更多信息請(qǐng)查看網(wǎng)絡(luò)編程
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2025國(guó)考·省考課程試聽(tīng)報(bào)名

    • 報(bào)班類型
    • 姓名
    • 手機(jī)號(hào)
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專用圖標(biāo)