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 .
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.
Online Demo || Download Source Code
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 .
HTML/CSS Form Design
The code snippet below contains the code that makes up the form design .
body {
font-family:Arial, Helvetica, sans-serif
}
#availability_status {
font-size:11px;
margin-left:10px;
}
input.form_element {
width: 221px;
background: transparent url('bg.jpg') no-repeat;
color : #747862;
height:20px;
border:0;
padding:4px 8px;
margin-bottom:0px;
}
label {
width: 125px;
float: left;
text-align: left;
margin-right: 0.5em;
display: block;
}
.style_form {
margin:3px;
}
#content {
margin-left: auto;
margin-right: auto;
width: 600px;
margin-top:200px;
}
#submit_btn {
margin-left:133px;
height:30px;
width: 221px;
}
<div id="content"> <form action="user_check.html" method="get"> <div> <label for="username">Username :</label> <input type="text" name="username" id="username"/> <span id="availability_status"></span> </div> <div> <label for="full_name">Full Name :</label> <input type="text" name="full_name" id="full_name"/> </div> <div> <label for="email">Email :</label> <input type="text" name="email" id="email"/> </div> <div> <input name="submit" type="submit" value="submit" id="submit_btn" /> </div> </form> </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.
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 <span> with id=”availability_status” .Almost every line of codes in commented below .
$(document).ready(function()//When the dom is ready
{
$("#username").change(function()
{ //if theres a change in the username textbox
var username = $("#username").val();//Get the value in the username textbox
if(username.length > 3)//if the lenght greater than 3 characters
{
$("#availability_status").html('<img src="loader.gif" align="absmiddle"> Checking availability...');
//Add a loading image in the span id="availability_status"
$.ajax({ //Make the Ajax Request
type: "POST",
url: "ajax_check_username.php", //file name
data: "username="+ username, //data
success: function(server_response){
$("#availability_status").ajaxComplete(function(event, request){
if(server_response == '0')//if ajax_check_username.php return value "0"
{
$("#availability_status").html('<img src="available.png" align="absmiddle"> <font color="Green"> Available </font> ');
//add this image to the span with id "availability_status"
}
else if(server_response == '1')//if it returns "1"
{
$("#availability_status").html('<img src="not_available.png" align="absmiddle"> <font color="red">Not Available </font>');
}
});
}
});
}
else
{
$("#availability_status").html('<font color="#cc0000">Username too short</font>');
//if in case the username is less than or equal 3 characters only
}
return false;
});
});
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″ .
include('database_connection.php');
//Include The Database Connection File
if(isset($_POST['username']))//If a username has been submitted
{
$username = mysql_real_escape_string($_POST['username']);//Some clean up :)
$check_for_username = mysql_query("SELECT userid FROM member WHERE username='$username'");
//Query to check if username is available or not
if(mysql_num_rows($check_for_username))
{
echo '1';//If there is a record match in the Database - Not Available
}
else
{
echo '0';//No Record Found - Username is available
}
}
That’s all Folks ! Got a Comment ? Criticism?Share it with us below !





{ 2 trackbacks }
{ 52 comments… read them below or add one }
great post as usual!
I really like the way you blog. This is simple, yet effective way of user friendly form.
Thanks Zvonko :)
Great work pal…
hey dude…..nice posts…I liked it..
sir how can apply this code in my php where i apply it
Like many of the forms/registration I’ve checked, this one has all kinds of errors in functioning. There are so many I won’t try to list them.
You get a D for putting something on the page.
Works great except it keeps saying ‘checking availability’. Everything looks good on my side. Any clues?
Nice tutorial. =)
great work!
This tutorial really helped me to do my assignment…thanks
Thanxxxxxxxxxxxxxxxx a lot
Thank you for such a wonderful article….Keep up the good work.
Thanks to your entire team,…….i have tried 100’s of websites on google but none of them helped me ……but the way u guys explained it was awesome……….hi5 to all of u…
thankkkkkkkkkkkkkkkkkkkkkkkkkkkkkk youuuuuuuuuuuuuuuuuuuu
@Max
1. u need to change the data base name in database_connection.php
2. Change table name in ajax_check_username.php
Then it will wrk fine …post again if u need anymore help
nice tut! very helpful. thanks
Its helpfull. But can i have compelete registration form, login page & protected sample page.
Raihan
Email: raihan_mazumder@yahoo.com
Thank you very much!
Good luck!
thanks i realllllllyyyyyy like it…..
Thank You..”You Hack Me”.. I searched this user availability code from many websites but.. am nt satisfied.. Thank You For Giving This code… Nw Am Really satisfied.. I book marked Your Website… Thank u
Great Work……………
Keep it up……………..
Thanx……………….
The tutorial is AWESOME. The only one from TENS I’ve tried to learn about this. I would like to ask you something though.
How do I make it so that I can have it check the email availability too? I’ve tried tweaking it, but I am a zero in this so unfortunately I didn’t come to anything :(
Thank you for your wonderful work.
Hi!
What software do you use to see the database?
I was searching about an software wich to do that, but none good I found…
Have a good day!
@SapioIT : Navicat for Mysql
Thanks a lot for such easy description..of flow.
Your tutorials is really easy to manage with..
Great job..keep it up
-anish
Thank you for this – a tutorial that is simply and easy to understand for a change and all the code makes sense even though I am not too good at JQuery/Ajax/Javascript.
This can be easily understand, but i have doubt how to store username,email,full_name
in MySQL database please clarified it
What’s the issue actually ? can you be more precise? you mean you don’t know how to insert records from php into mysql ?
Me too… I wonder where is the insert statement here… I checked the three pages but I can’t found it… and the whole code didn’t work on me…
wow, thank you. Now I can check a username….
Hi,
Works fine, but I too get the ‘checking username’ without it updating. The rest is working and in firebug I can see the server response being ‘0′. But the div isn’t updating. Any idea?
wow wonderful tutorial !
This stuff is real good…but can u tell me how to stop the form from submitting even if it shows userid not available!!!
Thnx a lot :)
great work!
Easy to understand………………
thnxxxxx
Great work ..
Really very helpful
thanks sir,
you provide to vitamin to every developer.. this gave cloud….
Great post keep up guys
Thanks very userfulllllllllllllllllllllllllllllllllllll very very userful and simple coding and understable
great work. i like it………………. Lot of thank….
nice tutorial ,…very helpful..
But the same validation i want to do for other fields like name,password,email etc..How it could b done ??
any idea??
I am confused how to include al fields in ajax call n validate,…..Plz respond,…
Thanq very much
Nice…
Hi,
Just found your site via a Google search – great work! Do you have the ‘Username Availability Check in Registration Form using Jquery/PHP’ code written to support checking Active Directory/LDAP instead of the SQL database?
Very happy to send a donation your way for some good code if you do.
Cheers,
Darren
Such a lovely post..
I don’t know why there are so many responses which constitute this post as good. I mean, look at the code… I stopped reading when I saw the first Tag. Furthermore, you use mysql_* stuff. Reminds of the old PHP4 days…
Please, ensure you use more up to date techniques. Even if this is only a proof of conception, there is no excuse using deprecated code…
I have used the above program to check the availability of username from database….
when i run that code the datas i had entered is getting store in the database..if i give the same name i have used before too its getting store in the database…..
Checking condition is not all working…
pls tell me if anyone knows how to do this code……
and i need to know to save this files and steps to run that file………
I liked your tutorial. But when I ran it on my website It just kept on checking for 3 hrs and i checked every bit of code still it did not come then i removed the single quotes from the server_response
ie., if(server_response == ‘0′) to if(server_response == 0)
Then your code worked like a charm. Just wrote this comment so if anybody who is having same problem can look at it.
saurabh
Great Tutorial.
I had some issues doe with leading spaces, which were easily solved by trimming:
if($.trim(server_response) ==
Thanks.
Thanks alot sir. very useful and attractive method.
Hi
i want to create a registration form something exactly like Yahoo ID registration form…. Where users can register and get a free email address from my email domain…
any links for the tut or modules in joomla
what a great post i ever seen………………………..
how to prevent submit from when username is not available?
Thanks this site is very nice
your tutorial is simple and easy to understand, I shall share it with my friends :)