Ajax detects if the user name is unique

Source: Internet
Author: User
We all know that a lot of Web page registration when the user name duplicate can not be registered, this article through the example code to introduce you to the AJAX application example of the detection of the user name is unique, very good, with reference value, the need for friends can refer to, hope to help to hit home.

First show you the next, and then give everyone the code, as follows:

Here is a simple example to detect if the user name is unique (direct code):

Front-End Interface:


<%@ page language= "java" contenttype= "text/html; charset=gb18030 "pageencoding=" GB18030 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

Ajax files:


<script language= "JavaScript" >function createrequest (URL) {http_request = false; if (window.             XMLHttpRequest) {//non ie browser http_request = new XMLHttpRequest (); Creates a XMLHttpRequest object} else if (window.  ActiveXObject) {//IE browser try {http_request = new ActiveXObject ("Msxml2.xmlhttp"); Create XMLHttpRequest Object} catch (e) {try {http_request = new ActiveXObject ("Microsoft.XMLHTTP");//Create XMLHTTP Request Object} catch (e) {}}} if (!http_request) {alert ("Cannot create XMLHttpRequest object instance!    ");  return false;            } http_request.onreadystatechange = GetResult;                The call returns the result handler function Http_request.open (' GET ', url, true);                    Create a connection to the server http_request.send (NULL); Send request to Server}function GetResult () {if (http_request.readystate = = 4) {//Determine request status if (Http_request.status = = 200)     {//request succeeded, start processing return result document.getElementById ("tooltip"). Innerhtml=http_request.responsetext;//Set prompt content document.getElementById ("ToolTip"). style.display= "Block"; Show Prompt Box} else {//Request page has error alert ("The page you requested has an error!    "); }}}function CheckUser (userName) {if (username.value== "") {alert ("Please enter the username!")  "); Username.focus (); return;  }else{createrequest (' checkuser.jsp?user= ' +username.value); }}</script>

JSP file:

This example does not connect to the database, but simply represents the registered user with an array.


<%@ page language= "java" import= "java.util.*" pageencoding= "GB18030"%><%  string[] userlist={"Tomorrow Technology", "Mr "," Mrsoft "," Wgh "};     Creates a one-dimensional array  of string User=new string (Request.getparameter ("user"). GetBytes ("Iso-8859-1"), "GB18030");//Get username  Arrays.sort (userlist);                 Sorts an array of   int result=arrays.binarysearch (userlist,user);       Search Array  if (result>-1) {    out.println ("Sorry, this username has already been registered! ");     Output test results  }else{    out.println ("Congratulations, this username is not registered! ");     Output test result  }%>

Like QQ dance, King Glory and other popular games when registering the user name can not be repeated, so this article is very valuable, and quickly collection it up.

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.