Ajax checks whether the user name exists

Source: Internet
Author: User
HTML code <Div class = "mtitle"> <span> User name: </span> </div>
<Div class = "mright"> <input name = "username" type = "text" id = "username" maxlength = "20" size = "20"
Class = "colorblue" onfocus = "This. classname = 'colorfocal ';"
Onblur = "This. classname = 'colorblu'; checkusername (this. Value);"/>
<Span id = "checkresult"> up to 20 characters </span> </div>

JavaScript code Function checkusername (username)
{
VaR unlen = username. Replace (/[^ \ x00-\ xFF]/g, "**"). length; // two characters are counted if the input is not an ASCII Value
If (unlen <3 | unlen> 20 ){
Document. getelementbyid ("checkresult"). innerhtml = "<font color = '#009900'>"
+ (Unlen <3? Profile_username_tooshort: profile_username_toolong) + "</font> ";
Return;
}
Ajaxread ("tools/ajax. aspx? T = checkusername & username = "+ escape (username ),
"Showcheckresult (OBJ, '" + username + "');");
}
// Escape
(Charstring) is Unicode conversion for string objects so that they can be read on all computers. All spaces, punctuation marks, accents, and other non-
ASCII characters are replaced by % XX encoding. XX indicates the hexadecimal number of the character. For example, "% 20" is returned by a space ". The character value must be greater than %
Uxxxx format storage. Unescape is a reverse operation.
// Obtain the original character in Ajax. aspx using the system. Web. httputility. urldecode () method
Function showcheckresult (OBJ, username)
{
VaR res = obj. getelementsbytagname ('result'); // search for the node of the result label in XML
VaR rescontainer = Document. getelementbyid ("checkresult ");
VaR result = "";
If (RES [0]! = NULL & res [0]! = Undefined)
{
If (RES [0]. childnodes. length> 1 ){
Result = res [0]. childnodes [1]. nodevalue;
} Else {
Result = res [0]. firstchild. nodevalue;
}
}
If (result = "1 ")
{
Rescontainer. innerhtml = "<font color = '#009900'> sorry, you entered the USERNAME \" "+ htmlencode (username, true, 4) + "\" has been used or disabled by others. Please select another name and try again. </Font> ";
}
Else
{
Rescontainer. innerhtml = profile_username_pass;
}
}

Function createxmlhttp (){
If (window. XMLHttpRequest ){
Return new XMLHttpRequest ();
} Else if (window. activexobject ){
Return new activexobject ("Microsoft. XMLHTTP ");
}
Throw new error ("XMLHTTP object cocould be created .");
}

Function ajaxread (file, fun ){
VaR xmlobj = createxmlhttp ();

Xmlobj. onreadystatechange = function (){
If (xmlobj. readystate = 4 ){
If (xmlobj. Status = 200 ){
OBJ = xmlobj. responsexml; // the value assigned to the OBJ variable is UTF-8 encoded.
Eval (fun); // execute the Javascript statement in the fun string
}
Else {
Alert ("error: [" + xmlobj. Status + "]");
}
}
}
Xmlobj. Open ('get', file, true );
Xmlobj. Send (null );
}

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.