During registration, the user name is often verified. If the entered user name already exists, a prompt is provided immediately instead of waiting for verification after the information is filled in. If there is a lot of input information, not only will the information be lost, it is annoying to perform a callback. You can use ajax to verify the registration to avoid these problems. The following is a simple example to determine whether the user name exists:
From: http://www.cnblogs.com/huazai/archive/2008/11/25/1340911.html
First: original JS Verification
HmtlCode:
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "head1" runat = "server">
<Title> </title>
<Script language = "JavaScript" type = "text/JavaScript" src = "checkusername. js"> </SCRIPT>
<Script language = "JavaScript">
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Table cellpadding = "0" cellspacing = "0" width = "100%">
<Tr>
<TD> Ajax example: </TD>
</Tr>
<Tr>
<TD>
<Table cellpadding = "0" cellspacing = "0" width = "100%">
<Tr>
<TD> Example 1: Check username isexists </TD>
</Tr>
<Tr>
<TD>
<Asp: Label id = "lblusername" runat = "server" text = "username:"> </ASP: Label>
<Input id = "txtusername" type = "text" onblur = "onblur ()"/>
</TD>
</Tr>
</Table>
</TD>
</Tr>
</Table>
</Form>
</Body>
</Html>
// Checkusername. js
VaR Ajax = function (option)
{
VaR request;
VaR createrequest = function ()
{
VaR request;
If (window. XMLHttpRequest)
{
Request = new XMLHttpRequest ();
}
Else
{
Try
{
Request = new activexobject ("Microsoft. XMLHTTP ");
}
Catch (E)
{
Request = new activexobject ("msxml2.xmlhttp ");
}
}
Return request;
}
VaR sendrequest = function ()
{
Request = createrequest ();
// Request. Open ("Post", option. url, true );
// Request. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ");
// Request. onreadystatechange = responserequest;
// Request. Send (option. Param );
Request. Open ("get", option. url + "? T = 0 ", true );
Request. onreadystatechange = responserequest;
Request. Send (null );
}
VaR responserequest = function ()
{
If (request. readystate = 4)
{
If (request. Status = 200)
{
Option. Success (request );
}
Else
{
Option. Failure (request );
}
}
}
Sendrequest ();
}
VaR onblur = function ()
{
VaR option = {URL: "test. aspx ", Param:" T = 1 ", success: function (request) {alert (request. responsetext) ;}, faitrue: function (request) {alert (false )}};
New Ajax (option );
}
// Test. aspx
Protected void page_load (Object sender, eventargs E)
{
If (request ["T"]! = NULL)
{
This. response. Clear ();
String T = request ["T"]. tostring ();
If (t = "1 ")
{
Response. Write ("the user name already exists. Please enter another user name! ");
}
Else if (t = "0 ")
{
Response. Write ("this user name is not registered. You can use it! ");
}
This. response. End ();
}
}
Method 2: Ajax verification using jquery framework
Hmtl code:
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
<SCRIPT src = "jquery-1.2.6.min.js" type = "text/JavaScript"> </SCRIPT>
<SCRIPT>
$ (Document). Ready (function (){
$ ("# Button1"). Click (function (){
$. Ajax ({
Type: "Get ",
URL: "responsepage. aspx? T = 0 ",
Datatype: 'html ',
Success: function (data ){
Alert ("Post" + data );
},
Error: function () {alert ('error! ');}
});
});
});
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Input id = "button1" type = "button" value = "display"/>
</Div>
</Form>
</Body>
</Html>
// Responsepage. apsx
Protected void page_load (Object sender, eventargs E)
{
This. response. Clear ();
String T = request ["T"]. tostring ();
If (t = "1 ")
{
Response. Write ("the user name already exists. Please enter another user name! ");
}
Else if (t = "0 ")
{
Response. Write ("this user name is not registered. You can use it! ");
}
This. response. End ();
}