JSP---Online shop, using Ajax technology to verify that the user name already exists

Source: Internet
Author: User

JS Code:

1<script language= "JavaScript" >2 var req;3 function Createreq () {4         if(window. XMLHttpRequest) {5req =NewXMLHttpRequest ();6}Else if(window. ActiveXObject) {7req =NewActiveXObject ("Microsoft.XMLHTTP");8         }9     }Ten  One function Validate () { A createreq (); -var id = document.getElementById ("CID"); -var url = "Validate.jsp?id=" +Escape (id.value); theReq.open ("Get", URL,true); -Req.onreadystatechange =callback; - req.send (); -     } +  - function Callback () { +         if(Req.readystate = = 4) { A             if(Req.status = = 200) { atvar msg = document.getElementById ("msg")); -msg.innerhtml =Req.responsetext; -             } -         } -     } -</script>

User-registered JSP page key HTML code

1 <!--input user name text box--2 <input type= "text" id= "CID" name= "name" onblur= "Validate ()"/>3 <!--display information span--4 <span id= "msg" style= "color:red;" ></span>

Code for the asynchronous request that is processed in the background (validate.jsp)

1<%2String id = request.getparameter ("id");3         if(id! =NULL) {4             if(Customermgr.getinstance (). Existname (ID)) {5Out.write ("The user name already exists!"));6}Else {7Out.write ("Congratulations, you can use the user name!"));8             }9 Ten         } One%>

Experience: When the user name text box is out of focus (onblur event), an asynchronous request object created by JS creates a request to the server and passes in the user name parameter, if the server is ready, receives the request and processes it, and returns the response information back to the client.

JSP---Online shop, using Ajax technology to verify that the user name already exists

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.