User name verified without refreshing Ajax (DOM + jquery)

Source: Internet
Author: User

Check whether the user name has been registered.

Dom version. This Code only supports ie browsers

<! 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">

If (text1! = "") {Var XMLHTTP = new activexobject ("Microsoft. XMLHTTP"); If (! XMLHTTP) {alert ("ajax error"); Return false;} XMLHTTP. Open ("Post", "checkusername. ashx? Username = "+ text1 +" & t = "+ new date ()," false "); XMLHTTP. onreadystatechange = function () {If (XMLHTTP. readystate = 4) {If (XMLHTTP. status = 200) {// alert (XMLHTTP. responsetext); If (XMLHTTP. responsetext = "OK") {document. getelementbyid ("SP1 "). innerhtml = "<font color = green> congratulations Username '" + text1 + "' available </font>";} else if (XMLHTTP. responsetext = "error") {document. getelementbyid ("SP1 "). innerhtml = "<font color = Red> Username '" + text1 + "' has been registered </font> ";} else {alert ("ajax return error") ;}} else {alert ("ajax server return error") ;}} XMLHTTP. send () ;}</SCRIPT>

Jquery version. This code is compatible with most browsers.

<! 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"> 

Server handler: checkusername. ashx

<%@ WebHandler Language="C#" Class="CheckUserName" %>using System;using System.Web;public class CheckUserName : IHttpHandler {        public void ProcessRequest (HttpContext context) {        context.Response.ContentType = "text/plain";        string us=context.Request["username"];        jiang_Db newdb = new jiang_Db();        bool hasname = newdb.ExecSql_Bool("select COUNT(*) from [user] where [user_name]='"+us+"'");        if (hasname)        {            context.Response.Write("error");        }        else        {            context.Response.Write("ok");        }    }     public bool IsReusable {        get {            return false;        }    }}

 

 

 

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.