AJAX user registration demo

Source: Internet
Author: User
Tags mysql tutorial php tutorial strlen
The code is as follows: Copy code
<! Doctype html public "-// w3c // dtd html 4.0 // en"
Http://www.w3.org/tr/rec-html140/strict.dtd>
<Html>
<Head>
<Meta http-equiv = "content-type" content = "text/html; charset = gb2312">
<Title> ajax user registration demo </title>
<Script language = "webpage effect" type = "text/webpage effect">
<! --
// Create a function
Function createxmlhttp ()
{
Var request;
Var browser = navigator. appname;
// If ie is used, the xmlhttp object is used.
If (browser = "microsoft internet explorer ")
  {
Var arrversions = ["microsoft. xmlhttp", "msxml2.xmlhttp. 4.0 ",
"Msxml2.xmlhttp. 3.0", "msxml2.xmlhttp", "msxml2.xmlhttp. 5.0"];
For (var I = 0; I <arrversions. length; I ++)
    {
Try
      {
// Locate a supported version and create an xmlhttp object
Request = new activexobject (arrversions [I]);
Return request;
      }
Catch (exception)
      {
// Ignore and continue
      }
    }
  }
Else
  {
// Otherwise, an xmlhttprequest object is returned.
Request = new xmlhttprequest ();
If (request. overridemimetype)
    {
Request. overridemimetype ('text/XML ');
}
Return request;
  }  
}
// Global xmlhttp object instance variable
Var http = createxmlhttp ();
// Send the request
Function chkuser ()
{
Var url = "check. php Tutorial"; // request "checkusername" servlet
Var name = document. getelementbyid ("username"). value;
Url + = ("? Username = "+ escape (name) +" & oprate = chkuser ");
Http. open ("get", url, true );
Http. onreadystatechange = processhttpresponse;
Http. send (null );
Return;
}
// Process the response
Function processhttpresponse ()
{
If (http. readystate = 4)
  {
If (http. status = 200)
    {
Var xmldocument = http. responsexml;
If (http. responsetext! = "This user name is valid and can be used! ")
      {
// Dynamic display of returned information
Document. getelementbyid ("showstr"). style. display = "";
Document. getelementbyid ("username"). style. background = "# ff0000 ";
Document. getelementbyid ("showstr"). innertext = http. responsetext;
}
Else
      {
Document. getelementbyid ("username"). style. background = "# ffffff ";
Document. getelementbyid ("showstr"). style. display = "";
Document. getelementbyid ("showstr"). innertext = http. responsetext;
}
    }
Else
    {
Alert ("the page you requested has an exception, which may affect your browsing of this page! ");
Alert (http. status );
    }
  }
}
// Check the input password
Function chkpassword ()
{
Var m = document. form1;
If (m. password. value. length> 20 | m. password. value. length <6)
  {
Document. getelementbyid ("passwordstr"). style. display = "";
Document. getelementbyid ("password"). style. background = "# ff0000 ";
Document. getelementbyid ("passwordstr"). innertext = "Sorry, the password must be a letter, number, or underline. The length is 6 ~ 20! ";
   }
Else
   {
Document. getelementbyid ("password"). style. background = "# ffffff ";
Document. getelementbyid ("passwordstr"). style. display = "none ";
   }
}
// Verify that the two passwords are consistent
Function chkconfirmpassword ()
{
Var m = document. form1;
If (m. password. value! = M. confirmpassword. value)
{
Document. getelementbyid ("confirmpasswordstr"). style. display = "";
Document. getelementbyid ("confirmpassword"). style. background = "# ff0000 ";
Document. getelementbyid ("confirmpasswordstr"). innertext = "Sorry, the password is inconsistent with the repeated password! ";
}
Else
{
Document. getelementbyid ("confirmpassword"). style. background = "# ffffff ";
Document. getelementbyid ("confirmpasswordstr"). style. display = "none ";
}
}
// Verify whether the email is valid
Function chkemail ()
{
Var m = document. form1;
Var email = m. email. value;
// Regular expression
Var regex =/^ ([A-Za-z0-9 _-]) + @ ([A-Za-z0-9 _-]) + (. [A-Za-z0-9 _-]) + /;
Var flag = regex. test (email );
If (! Flag)
{
Document. getelementbyid ("emailstr"). style. display = "";
Document. getelementbyid ("email"). style. background = "# ff0000 ";
Document. getelementbyid ("emailstr"). innertext = "Sorry, invalid email address! ";
}
Else
{
Document. getelementbyid ("email"). style. background = "# ffffff ";
Document. getelementbyid ("emailstr"). style. display = "none ";
}
  
}
// Submit the check function
Function submitcheck ()
{
Var m = document. form1;
If (m. username. value. length = 0)
  {
Alert ("Sorry, the username must be an English letter, number, or underline. The username length is 5 ~ 20. ");
M. username. focus ();
Return false;
  }
If (m. password. value. length = 0)
  {
Alert ("Sorry, the password must be a letter, number, or underline. The password length is 5 ~ 20. ");
M. password. focus ();
Return false;
  }
If (m. password. value! = M. confirmpassword. value)
  {
Alert ("Sorry, the password is inconsistent with the repeated password! ");
M. confirmpassword. focus ();
Return false;
  }
If (m. email. value. length = 0)
  {
Alert ("Sorry, the email address cannot be blank !! ");
M. email. focus ();
Return false;
  }
M. submit ();
}
// -->
</Script>
<Body>
<Form name = "form1" method = "post" action = "register. php">
<H3 align = "center"> ajax user registration program <Table align = "center" width = "500" border = "1">
<Tr>
<Td> <font color = "red"> * </font> </td>
<Td width = "100"> user account: </td>
<Td> <input type = "text" name = "username" maxlength = "20" style = "background = # ffffff" onblur = "chkuser ()"> </td>
<Td> <div id = "showstr" style = "background-color: # ff9900; display: none"> </div> </td>
</Tr>
<Tr>
<Td> <font color = "red"> * </font> </td>
<Td> user password: </td>
<Td align = "left"> <input type = "password" name = "password" maxlength = "22" style = "background = # ffffff" onblur = "chkpassword () "/> </td>
<Td> <div id = "passwordstr" style = "background-color: # ff9900; display: none"> </div> </td>
</Tr>
<Tr>
<Td> <font color = "red"> * </font> </td>
<Td> Confirm password: </td>
<Td> <input type = "password" name = "confirmpassword" maxlength = "20" style = "background = # ffffff" onblur = "chkconfirmpassword () "/> </td>
<Td> <div id = "confirmpasswordstr" style = "background-color: # ff9900; display: none"> </div> </td>
</Tr>
<Tr>
<Td> <font color = "red"> * </font> </td>
<Td> email: </td>
<Td> <input type = "text" name = "email" maxlength = "100" style = "background = # ffffff" onblur = "chkemail ()"> </td>
<Td> <div id = "emailstr" style = "background-color: # ff9900; display: none"> </div> </td>
</Tr>
</Table>
<Div align = "center">
 
<Input type = "button" name = "OK" value = "OK" onclick = "submitcheck ()">
<Input type = "reset" name = "reset" value = "cancel">
</Form>
</Div>
</Body>
</Html>

Reg. php detection program

The code is as follows: Copy code

<? Php
Header ("content-type: text/html; charset = gb2312 ");
@ Mysql tutorial _ connect ('localhost', 'root', 'ebaeba ') or die ("database tutorial server connection failed ");
@ Mysql_select_db ("test") or die ("The database does not exist or is unavailable ");

 

$ Uname = $ _ get ['username'];
// Perform the following database query to check whether this user exists.
// If this user name is not found

 

$ SQL = "select * from t1 where name = '". $ uname ."'";
$ Query = mysql_query ($ SQL );
$ Row = mysql_fetch_object ($ query );

If (strlen ($ uname) <6 | strlen ($ uname)> 20)
{
$ Msg = "the user name must be 6 to 20 characters .";
}
Else
{
 
If ($ row = false)
   {
$ Msg = "this user name is valid and can be used! ";
   }
Else
   {
$ Msg = "Sorry, this user name already exists. Please register with another user name! ";
   }
}
Echo $ msg;
?>

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.