Ajax checks whether the user name is occupied

Source: Internet
Author: User

Copy codeThe Code is as follows:
// Check whether the user name is occupied
Function GetO (){
Var ajax = false;
Try {
Ajax = new ActiveXObject ("Msxml2.XMLHTTP ");
} Catch (e ){
Try {
Ajax = new ActiveXObject ("Microsoft. XMLHTTP ");
} Catch (E ){
Ajax = false;
}
}
If (! Ajax & typeof XMLHttpRequest! = 'Undefined '){
Ajax = new XMLHttpRequest ();
}
Return ajax;
}
Function getResult (serverPage ){
Var ajax = GetO ();
Var sep = (-1 <serverPage. indexOf ("? "))? "&":"? "
Var newurl = serverPage + sep + "__=" + encodeURIComponent (new Date (). toString ());

Ajax. open ("GET", newurl, true );
Ajax. onreadystatechange = function (){
If (ajax. readyState = 4 & ajax. status = 200 ){
If (ajax. responseText = 1 ){
ShowResult (1, "your user name can be registered normally ")
AjaxResult = true;
} Else {
ShowResult (0, "your user name is occupied ")
AjaxResult = false;
}
}
}
Ajax. send (null );

}
Function chkUsername (){
Var sflag; // tag;
Var sErr; // error message;
Sflag = true;

Var re =/^ \ w {2, 20} $ /;
If (! Re. test (document. Form1.UserName. value )){
Sflag = false;
SErr = "the user name can only contain letters, numbers, or underscores ";
}

If (document. Form1.UserName. value. length> 20 | document. Form1.UserName. value. length <2 ){
Sflag = false;
SErr = "username length must be 2 ~ Between 20 characters ";
}
If (document. Form1.UserName. value = ""){
SErr = "username cannot be blank ";
Sflag = false;
}
// Ajax detection returned results
If (! Sflag ){
ShowResult (0, sErr)
} Else {
GetResult ("../MemberCheck. aspx? T = 1 & Name = "+ document. Form1.UserName. value );
}

// Document. getElementById ("spname"). innerHTML = ss;
}
Function ShowResult (tag, str ){
Var obj = document. getElementById ("spname ");
If (tag = 0 ){
Obj. style. border = "1px solid red ";
Obj. style. color = "red ";

} Else {
Obj. style. border = "1px solid green ";
Obj. style. color = "green ";
}
Obj. innerHTML = str;
Obj. style. padding = "1px ";
}
// Obtain the user question value returned by ajax
Function getQuestionData (sPage ){
Var ajax = GetO ();
Var sep = (-1 <sPage. indexOf ("? "))? "&":"? "
Var newurl = sPage + sep + "__=" + encodeURIComponent (new Date (). toString ());

Ajax. open ("GET", newurl, true );
Ajax. onreadystatechange = function (){
If (ajax. readyState = 4 & ajax. status = 200 ){
If (ajax. responseText = ""){
Document. getElementById ("getstate"). innerHTML = "the user does not exist! "
}
Else {
Document. g. Question. value = ajax. responseText;
Document. g. Question. readOnly = true;
Document. getElementById ("getpwdstep1"). style. display = "none ";
Document. getElementById ("getpwdstep2"). style. display = "block ";
Document. getElementById ("getstate"). innerHTML = "";

}
}
}
Ajax. send (null );
}
Function getUserQ (){
// Change the display style of the error span
Document. getElementById ("getstate"). style. padding = "1px 4px ";
If (document. g. UserName. value = ""){
Document. getElementById ("getstate"). innerHTML = "the user name cannot be blank! "
} Else {
Document. getElementById ("getstate"). style. background = "red ";
Document. getElementById ("getstate"). style. color = "white ";
Document. getElementById ("getstate"). innerHTML = "loading data ...";
// Set ajax
GetQuestionData ("../MemberCheck. aspx? T = 2 & gName = "+ document. g. UserName. value );
}
}

Function login (n, p, t)
{
If (t = false ){
Document. getElementById ("pLogin"). style. display = "none ";
Document. getElementById ("LoginInfo"). innerHTML = "logging in for you. Please wait ...";
}
Var request;
If (undefined = request ){
Request = new Request ();
}

If (undefined = request. _ get ){
// Ajax is not supported to obtain data;
If (t = false ){
Document. getElementById ("pLogin"). style. display = "";
Document. getElementById ("LoginInfo"). innerHTML = "data cannot be obtained ";
}
}
Else {
UserName = n;
ToPrev = t;
Request. GetNoCache (loginPath + "LoginCheck. aspx" + "? N = "+ n +" & p = "+ p, loginCheck );
}
}
Function loginCheck (result)
{
If (result. readyState! = ReadyState. Complete)
Return;
// Obtained successfully;
If (result. status = HttpStatus. OK & result. responseText! = "")
{
// Execute the returned statement;
// Alert (result. responseText );
If (result. responseText = '1 ')
{
If (toPrev = false ){
Document. getElementById ("LoginInfo "). innerHTML = '<ul> <li> Welcome' + userName + '</li> <a href = "' + loginPath + 'Member/"> management </> </li> <a href = "'+ loginPath +' Logout. aspx "> exit </a> </li> </ul> ';
}
Else {
If (UrlPrev! = ''){
Document. location. href = UrlPrev;
}
Else {
Document. location. href = loginPath;
}
}
Return;
}
Else
{
If (document. location. href! = "Http: //" + document. location. hostname + loginPath + "Member/Login. aspx ")
Document. location. href = loginPath + "Member/Login. aspx ";
}
}
Else
{
// Failed;
Document. location. href = loginPath + "Member/Login. aspx ";
Return;
}
}
Function checkKey (caputureKey, n, p, t ){
Var key = window. event. keyCode;
If (key = caputureKey ){
Login (n, p, t );
}
}
Function setLoginInfo (bLogin ){
If (bLogin ){
Document. getElementById ("LoginInfo "). innerHTML = '<ul> <li> Welcome' + userName + '</li> <a href = "' + loginPath + 'Member/"> management </> </li> <a href = "'+ loginPath +' Logout. aspx "> exit </a> </li> </ul> ';
}
Else {
}
}
Function gotoUrl (s ){
Window. location. href = loginPath + 'Member/RegUser.html ';
}

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.