Web registration page Dynamic matching authentication user authentication PHP implementation of a

Source: Internet
Author: User
Tags php database php regular expression
This article mainly introduces the Web registration page dynamic matching verification of the user authentication PHP implementation of a, has a certain reference value, now share to everyone, there is a need for friends can refer to

Knowledge Required: 1.php database connection

2.php Regular expression matching

3.ajax--

    • HTML and CSS Basics

    • JavaScript Basics

1:

Create a good input box:

<input name= "username" type= "text" class= "Form-control" id= "Contact_username" onkeyup= "Loadxmldoc (this.value)" placeholder= "User name" required/>            <span id= "Txthint" ></span>

2:

JS Implementation hints Effect: (Source: Click to open the link)

function Loadxmldoc (str) {    if (str.length==0)    {        document.getElementById ("Txthint"). innerhtml= "";        return;    }    var xmlhttp;    Check if the browser supports the XMLHttpRequest object    if (window. XMLHttpRequest)    {        //ie7+, Firefox, Chrome, Opera, Safari execute code        xmlhttp=new XMLHttpRequest ();    }    else    {        //IE6, IE5 Browser execution code        xmlhttp=new activexobject ("Microsoft.XMLHTTP");    }    Xmlhttp.onreadystatechange=function ()    {        if (xmlhttp.readystate==4 && xmlhttp.status==200)        {            //responsetext Gets the response data in the form of a string.            document.getElementById ("Txthint"). Innerhtml=xmlhttp.responsetext;        }    }    Xmlhttp.open ("GET", "..") /common/verify.php?v= "+str,true);    Xmlhttp.send ();}

3:

PHP code to implement Dynamic validation: (validation is flawed, because it is written by himself, there are many places is not perfect, I will continue to improve)

<?php//Registration Verification----------$v =trim ($_get[' V ');  Get user-entered account $hint = ""; Used as the return output//To determine whether the account or password, or other match//determine the input account length is greater than 0 if (strlen ($v) > 0) {//user authentication//1. Must start with a letter if (                Preg_match ("/^[a-z]/", $v)) {//2. At least 5 characters if (Preg_match ("/^[a-z][\w]{4}/", $v)) {                Echo $v;  $hint = "";                When satisfied, let it enter empty because the front does not meet the assigned value//database to establish a connection require "mysqli.php";                Database query statement--Query the input account exists $sql = "Select ' username ' from ' user ' where ' username ' = ' $v '";                $result = Mysqli_query ($conn, $sql); When Mysqli_num_rows ($result) > Description found Data if (Mysqli_num_rows ($result) > 0) {$hint = "the user already exists!                ";                }else{$hint = "This user is available"; } mysqli_close ($conn);            Close database connection}else{$hint = "At least 5 characters!"; }}else{$hint = "must start with a letter!"        "; }}if($hint = = "") {$response = "";} else{$response = $hint;} Print back echo $response;? >
 update: (June 5, 2018 14:31:00) 
Determine if the input account length is greater than 0if (strlen ($v) > 0) {//user authentication//1. Must start with a letter if (Preg_match ("/^[a-z]/", $v)) {//2. At least 5 characters        No longer than 11 characters if (strlen ($v) < 5 | | strlen ($V) > One) {$hint = "At least 5 characters, maximum no more than 11 characters!";                } else {//3. Pattern matches if (Preg_match ("/^[a-z][\w]{2,10}$/", $v)) {echo $v;  $hint = "";                When satisfied, let it enter empty because the front does not meet the assigned value//database to establish a connection require "mysqli.php";                Database query statement--Query the input account exists $sql = "Select ' username ' from ' user ' where ' username ' = ' $v '";                $result = Mysqli_query ($conn, $sql);  When Mysqli_num_rows ($result) > 0 description found Data if (Mysqli_num_rows ($result) > 0) {$hint = "The user already exists!"                ";                } else {$hint = "This user is available"; } mysqli_close ($conn);            Close database connection}else {$hint = "user name can only be numeric, letter, underline"; }}} else {$hint = "Must start with a letter!"    "; }}

4:

Fully from their own understanding of the interpretation of their own oral expression, not professional enough to be standard, if there is a mistake place welcome to point out: QQ363491343

Explanation: When we enter the user name to register in the username input box,

The function calls the Loadxmldoc () method (see step Two) for the relevant dynamic validation (see Click to open link for details)


PHP Code Explanation:

1.

$v =trim ($_get[' V ');

The user name used to obtain the user's dynamic input;

2.

if (strlen ($v) > 0)

The user must enter a user name that is greater than 0, which is the most basic.

3.

if (Preg_match ("/^[a-z]/", $v)) {

Then I specify that the user name must start with a letter, not case-sensitive

4.

This is the user name I want at least 5 characters;

5.

echo $v; $hint = "";  When satisfied, let it enter empty because the previous assignment is not satisfied//database to establish a connection require "mysqli.php";//Database query statement--Query the input account exists $sql= "Select ' username ' from ' user ' where ' Username ' = ' $v ' "; $result = Mysqli_query ($conn, $sql);//When Mysqli_num_rows ($result) > description found in Data if (Mysqli_num_rows ($ Result) > 0) {    $hint = "The user already exists! ";} else{    $hint = "The user is available";} Mysqli_close ($conn); To close a database connection

This part is the current surface is satisfied when, in the database dynamic search, to see if the user already exists.

6.

if ($hint = = "") {    $response = "";} else{    $response = $hint;} Print back echo $response;

The final output returns the content to be displayed;

Here I have verified by myself, in PHP inside the echo output is the content of the return.

If PHP has a warning message, or if an error occurs, it will go back to the front end.

If you are good at Ajax, then the following code is good to understand:

Xmlhttp.onreadystatechange=function ()    {        if (xmlhttp.readystate==4 && xmlhttp.status==200)        {            //responsetext Gets the response data in the form of a string.            document.getElementById ("Txthint"). Innerhtml=xmlhttp.responsetext;        }    }


Well, the above is my own written user account through the server to achieve dynamic verification effect, but also learn to do, I hope to help the same as my novice classmates.

If there is a big guy, please point out the mistakes and shortcomings.


-----New Additions:

Hide cue message when focus leaves the input box
   When the focus leaves the input box, hide the hint message function uppercase () {    document.getElementById ("Txthint"). innerhtml= "";}

In

<input name= "username" type= "text" class= "Form-control" id= "Contact_username" onkeyup= "Loadxmldoc (this.value)" Onblur= "uppercase ()" placeholder= "User name" Required/><span id= "Txthint" ></span>

Add the same onblur event inside

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.