Post image for Username Availability Check in Registration Form using Jquery/PHP

Username Availability Check in Registration Form using Jquery/PHP

by Hyder on May 4, 2010

in Jquery, PHP

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.

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 .

Username is not Available

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.

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 <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">&nbsp;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 !

Related Posts

  1. Create fancy contact form with CSS 3 and jQuery
  2. Designing a form using pure CSS enhanced with Jquery
  3. Building A registration System with Email verification in PHP
  4. Creating a Fancy Search Feature with PHP,Mysql and Jquery
  5. An Alternative to Pagination : Facebook and Twitter Style
After Post

{ 2 trackbacks }

Username Availability Check in Registration Form using Jquery/PHP Check body on me
May 4, 2010 at 4:01 am
Tweets that mention Username Availability Check in Registration Form using Jquery/PHP -- Topsy.com
May 4, 2010 at 8:15 am

{ 52 comments… read them below or add one }

TomPier May 6, 2010 at 1:58 pm

great post as usual!

Reply

Codeforest May 13, 2010 at 8:49 am

I really like the way you blog. This is simple, yet effective way of user friendly form.

Reply

Hyder May 14, 2010 at 8:34 am

Thanks Zvonko :)

Reply

Zemo Zamster July 27, 2010 at 3:55 pm

Great work pal…

Reply

ajay August 16, 2010 at 2:49 am

hey dude…..nice posts…I liked it..

Reply

ramesh September 11, 2010 at 3:16 pm

sir how can apply this code in my php where i apply it

Reply

david kjoller October 16, 2010 at 10:18 pm

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.

Reply

Max November 9, 2010 at 3:46 pm

Works great except it keeps saying ‘checking availability’. Everything looks good on my side. Any clues?

Reply

feenzy November 11, 2010 at 2:49 pm

Nice tutorial. =)

Reply

Fabio December 2, 2010 at 8:29 pm

great work!

Reply

arun ak December 21, 2010 at 4:30 am

This tutorial really helped me to do my assignment…thanks

Reply

fadia February 4, 2011 at 7:48 pm

Thanxxxxxxxxxxxxxxxx a lot

Reply

Sandipan February 12, 2011 at 1:28 pm

Thank you for such a wonderful article….Keep up the good work.

Reply

aman February 22, 2011 at 8:01 pm

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

Reply

aman February 22, 2011 at 8:04 pm

@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

Reply

ElYair February 24, 2011 at 11:43 am

nice tut! very helpful. thanks

Reply

Mohammad Raihan Mazumder March 5, 2011 at 5:08 am

Its helpfull. But can i have compelete registration form, login page & protected sample page.

Raihan
Email: raihan_mazumder@yahoo.com

Reply

QuocNha April 11, 2011 at 9:58 am

Thank you very much!
Good luck!

Reply

Nitishpal May 9, 2011 at 12:21 pm

thanks i realllllllyyyyyy like it…..

Reply

Mohamed May 11, 2011 at 10:21 am

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

Reply

ketan June 2, 2011 at 7:22 am

Great Work……………

Keep it up……………..

Thanx……………….

Reply

dojo June 19, 2011 at 4:21 pm

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.

Reply

SapioIT June 26, 2011 at 9:39 pm

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!

Reply

Hyder June 27, 2011 at 12:37 am

@SapioIT : Navicat for Mysql

Reply

Anish June 29, 2011 at 1:39 am

Thanks a lot for such easy description..of flow.
Your tutorials is really easy to manage with..
Great job..keep it up

-anish

Reply

Emma July 19, 2011 at 7:02 pm

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.

Reply

Neeraj August 3, 2011 at 10:09 am

This can be easily understand, but i have doubt how to store username,email,full_name
in MySQL database please clarified it

Reply

Hyder August 4, 2011 at 5:44 am

What’s the issue actually ? can you be more precise? you mean you don’t know how to insert records from php into mysql ?

Reply

UmiSakurai November 24, 2011 at 3:39 am

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…

Reply

getaufan September 14, 2011 at 12:17 pm

wow, thank you. Now I can check a username….

Reply

tj September 27, 2011 at 10:13 am

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?

Reply

jak October 16, 2011 at 2:29 pm

wow wonderful tutorial !

Reply

Mak November 11, 2011 at 9:26 am

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 :)

Reply

zagham November 24, 2011 at 1:38 pm

great work!

Easy to understand………………

thnxxxxx

Reply

shailesh November 30, 2011 at 1:16 pm

Great work ..
Really very helpful

Reply

Manoj January 30, 2012 at 11:33 am

thanks sir,

you provide to vitamin to every developer.. this gave cloud….

Reply

zaheer February 3, 2012 at 4:07 pm

Great post keep up guys
Thanks very userfulllllllllllllllllllllllllllllllllllll very very userful and simple coding and understable

Reply

Jestin Thomas February 13, 2012 at 12:02 pm

great work. i like it………………. Lot of thank….

Reply

bindu February 22, 2012 at 1:47 pm

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

Reply

Shashi February 27, 2012 at 7:55 am

Nice…

Reply

Darren Latter March 13, 2012 at 9:43 am

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

Reply

Tayo pat May 10, 2012 at 8:13 am

Such a lovely post..

Reply

Anonymous July 24, 2012 at 12:51 pm

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…

Reply

Jay August 3, 2012 at 10:26 am

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………

Reply

Saurabh Gupta August 8, 2012 at 11:28 pm

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

Reply

Pat August 26, 2012 at 4:49 am

Great Tutorial.
I had some issues doe with leading spaces, which were easily solved by trimming:
if($.trim(server_response) ==

Thanks.

Reply

ubaidullah August 27, 2012 at 7:58 pm

Thanks alot sir. very useful and attractive method.

Reply

vish September 6, 2012 at 5:15 am

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

Reply

Naeem Hassan Khan September 9, 2012 at 6:04 pm

what a great post i ever seen………………………..

Reply

halotala September 21, 2012 at 6:49 am

how to prevent submit from when username is not available?

Reply

byambaa September 22, 2012 at 2:48 pm

Thanks this site is very nice

Reply

Muhammad Hafiz September 25, 2012 at 8:13 am

your tutorial is simple and easy to understand, I shall share it with my friends :)

Reply

Leave a Comment

Previous post:

Next post: