Ajax implementation of asynchronous refresh to verify that the user name is already in a specific way

Source: Internet
Author: User

  Because to make a registration page, see many Web sites are using Ajax asynchronous refresh to verify that the user name is available, so do a small example yourself

Are simple examples, so direct send code   static page ajax.html   code as follows: <html>     <head>         < title>ajax</title>         <script type= "Text/javascript" >             Function Loadxmldoc () {                if (document.geteleme Ntbyid ("Account"). Value = = "" "{                    Document.getelement Byid ("Accdiv"). InnerHTML = "User name cannot be empty";                     return;                                 var XMLH Ttp                 if window. XMLHttpRequest) {//code for ie7+                     xmlHttp = new XML HttpRequest ();                }     &nbsp           ELSE {//code for IE5/IE6                 &NBS P   XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");                                   X Mlhttp.onreadystatechange = function () {                    if (xmlhtt P.readystate = 4 && xmlhttp.status = {                  &NBSP ;    //document.getelementbyid ("mydiv"). Innerhtml=xmlhttp.responsetext;                         if (Xmlhttp.responsetext = = "true") {                             document.getElementById ( "Accdiv"). InnerHTML = "User name not available";                        } &nbsp                       else {          &NB Sp                 document.getElementById ("Accdiv") InnerHTML = "User name available";                                   & nbsp        }                                 var a = document.getElementById ("account"). Value;                //Get                 XML Http.open ("Get", "validate.aspx?account=" + A + "&random=" + Math.random, True);                 xmlhttp.send ();                         function Deldata () {    & nbsp          document.getElementById ("account"). Value = "";                 document.getElementById ("Accdiv"). InnerHTML = "";                     </script>     </head>     <body>         <h3>ajax</h3>         <table> &nbsp ;           <tr>                 <td> account: </td ><td><input id= "Account" type= "text" onblur= "Loadxmldoc ();" onfocus= "Deldata ();" /></td><td><div id= "Accdiv" ></div></td>             </tr>             <tr>                 & lt;td> Password: </td><td><input id= "passwd" type= "password"/></td>         & nbsp   </tr> &nbsp           <tr>                 <td> Confirm password: </ Td><td><input id= "vpasswd" type= "password"/></td>             </ tr>             <tr>                 <TD > Name: </td><td><input id= "name" type= "text"/></td>             & lt;/tr>         </table>       </body> </html>     in account input When the box loses focus, the call function   Access server uses a Get method, so additional random code is used at the parameter to avoid caching.   Verification page validate.aspx Background code: The code is as follows: 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)     {        RESP Onse. Clear ();         if (Exists (request.querystring["account"))             Response . Write ("true");         else             Response.Write ("false");         Response.End ();    }    ///<summary>    ///Get database connection    ///</summary>   &nbs P <returns></returns>     protected SqlConnection getconnection ()     {    &N Bsp   String str = configurationmanager.connectionstrings["ConnectionString"]. ConnectionString;         conn = new SqlConnection (str);         return conn;    }     protected bool Exists (string account)     {       using (getconnection ())         {            try   & nbsp         {                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)               & nbsp     return true;                 else                 &NBSP ;   return false;                         catch (Exception e)     &N Bsp &nBsp     (                throw e;                         finally             {      &NBS P         Conn. Close ();             {       }    }}       verifying username in the background Whether it already exists in the database, return TRUE or false   database is very simple, only built a table userinfo, there are 3 fields: Account, passwd, name   NOTE: When writing data to the request page in the background, after writing the data to be sent, You need to call the Response.End () method to terminate the write, or you may send a full page past.  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.