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

Username is not Available
HTML/CSS Form Design
The code snippet below contains the code that makes up the form design .
02 | font-family : Arial , Helvetica , sans-serif |
10 | background : transparent url ( 'bg.jpg' ) no-repeat ; |
02 | < form action = "user_check.html" method = "get" > |
04 | < label for = "username" >Username :</ label > |
05 | < input type = "text" name = "username" id = "username" /> |
06 | < span id = "availability_status" ></ span > </ div > |
08 | < label for = "full_name" >Full Name :</ label > |
09 | < input type = "text" name = "full_name" id = "full_name" /> |
12 | < label for = "email" >Email :</ label > |
13 | < input type = "text" name = "email" id = "email" /> |
16 | < input name = "submit" type = "submit" value = "submit" id = "submit_btn" /> |
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 () |
03 | $( "#username" ).change( function () |
06 | var username = $( "#username" ).val(); |
07 | if (username.length > 3) |
09 | $( "#availability_status" ).html( ' Checking availability...' ); |
14 | url: "ajax_check_username.php" , |
15 | data: "username=" + username, |
16 | success: function (server_response){ |
18 | $( "#availability_status" ).ajaxComplete( function (event, request){ |
20 | if (server_response == '0' ) |
22 | $( "#availability_status" ).html( ' Available ' ); |
25 | else if (server_response == '1' ) |
27 | $( "#availability_status" ).html( ' Not Available ' ); |
39 | $( "#availability_status" ).html( 'Username too short' ); |
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″ .
01 | include ( 'database_connection.php' ); |
04 | if (isset( $_POST [ 'username' ])) |
06 | $username = mysql_real_escape_string( $_POST [ 'username' ]); |
08 | $check_for_username = mysql_query( "SELECT userid FROM member WHERE username='$username'" ); |
11 | if (mysql_num_rows( $check_for_username )) |
That’s all Folks ! Got a Comment ? Criticism?Share it with us below !
No comments:
Post a Comment