MY mENU


Tuesday 4 September 2012

Username Availability Check in Registration Form using Jquery/PHP


Post image for Username Availability Check in Registration Form using Jquery/PHP
  • digg
  • 62
     
    Share
Very often while registering on a website ,you will notice that they usually have a username or email address availability check. This ensures that 2 users do not have the same username or email address. Some websites prefer to make this availability check when a user fill in all the details in the form and press submit while some other websites do a live username / Email Address Availability check when the “username” textbox loses focus. Personally, I would prefer to let the user know if a particular username is available while filling the registration form rather than after submitting the registration form.
Below are some screenshots  from the most popular websites ( gmail.com , yahoo.com and hotmail.com ) that have email address verification before the user submits the form .
Email address verification at Hotmail
Email address verification at Yahoo Mail
Email address verification at gmail.com

What will you learn in this tutorial ?

This tutorial teaches you how to do a live username availability check using Jquery. The ajax request is triggered when the “username” textbox loses focus.  A loading image is added next to the textbox during this  asynchronous request .  After it  has completed,  an icon will replace the loading image based on whether the username is available or not.3//*
Credits
The Background image in the textbox was generated from this Form Elements Generator . The loading Image is taken from Preloaders .Other icons are taken from IconsPedia .
Username is not Available

HTML/CSS Form Design

The code snippet below contains the code that makes up the form design .
01body {
02 font-family:ArialHelveticasans-serif
03}
04#availability_status {
05 font-size:11px;
06 margin-left:10px;
07}
08input.form_element {
09 width221px;
10 backgroundtransparent url('bg.jpg'no-repeat;
11 color : #747862;
12 height:20px;
13 border:0;
14 padding:4px 8px;
15 margin-bottom:0px;
16}
17label {
18 width125px;
19 floatleft;
20 text-alignleft;
21 margin-right0.5em;
22 displayblock;
23}
24.style_form {
25 margin:3px;
26}
27#content {
28 margin-leftauto;
29 margin-rightauto;
30 width600px;
31 margin-top:200px;
32}
33#submit_btn {
34 margin-left:133px;
35 height:30px;
36 width221px;
37}
01<div id="content">
02 <form action="user_check.html" method="get">
03 <div>
04 <label for="username">Username :</label>
05 <input type="text" name="username" id="username"/>
06 <span id="availability_status"></span> </div>
07 <div>
08 <label for="full_name">Full Name :</label>
09 <input type="text" name="full_name" id="full_name"/>
10 </div>
11 <div>
12 <label for="email">Email  :</label>
13 <input type="text" name="email" id="email"/>
14 </div>
15 <div>
16 <input name="submit" type="submit" value="submit" id="submit_btn" />
17 </div>
18 </form>
19</div>

The Database Design

For the sake of simplicity ,i will not include the php database connection code here . You can download the full source code from the link provided above.
Database Structure

Jquery Code

This section contains the jquery Code that handles the Ajax request when the textbox loses focus .Based on the server response , the appropriate icon is appended to the with id=”availability_status” .Almost every line of codes in commented below .
01$(document).ready(function()//When the dom is ready
02{
03$("#username").change(function()
04//if theres a change in the username textbox
05 
06var username = $("#username").val();//Get the value in the username textbox
07if(username.length > 3)//if the lenght greater than 3 characters
08{
09$("#availability_status").html(' Checking availability...');
10//Add a loading image in the span id="availability_status"
11 
12$.ajax({  //Make the Ajax Request
13 type: "POST",
14 url: "ajax_check_username.php",  //file name
15 data: "username="+ username,  //data
16 success: function(server_response){
17 
18 $("#availability_status").ajaxComplete(function(event, request){
19 
20 if(server_response == '0')//if ajax_check_username.php return value "0"
21 {
22 $("#availability_status").html(' Available   ');
23 //add this image to the span with id "availability_status"
24 }
25 else  if(server_response == '1')//if it returns "1"
26 {
27 $("#availability_status").html(' Not Available ');
28 }
29 
30 });
31 }
32 
33 });
34 
35}
36else
37{
38 
39$("#availability_status").html('Username too short');
40//if in case the username is less than or equal 3 characters only
41}
42return false;
43});
44});

PHP Code

As you have probably noticed ,  the file “ajax_check_username.php” is called via ajax and the username is passed to the server . The code below query our database to check if the username is already in our database or not . It will either return the value “0″ or “1″ .
01include('database_connection.php');
02//Include The Database Connection File
03 
04if(isset($_POST['username']))//If a username has been submitted
05{
06$username = mysql_real_escape_string($_POST['username']);//Some clean up :)
07 
08$check_for_username = mysql_query("SELECT userid FROM member WHERE username='$username'");
09//Query to check if username is available or not
10 
11if(mysql_num_rows($check_for_username))
12{
13echo '1';//If there is a  record match in the Database - Not Available
14}
15else
16{
17echo '0';//No Record Found - Username is available
18}
19}
That’s all Folks ! Got a Comment ? Criticism?Share it with us below !