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; } }}