Live Username Availability Checking using Ajax and jQuery

Live Username Availability Checking using Ajax and jQuery. Username and password is widely used in any modern web site right now. Username is unique, it must be different with others. Therefore, we as developers have to write a code to check whether a username is already been taken or not yet.
I wrote simple code to do live check username availability using jQuery and Ajax.Live means you don’t need to click the submit button then send username into another code that returns availability of username. You can customize the code to better suit your need.

Table Structure

1
2
3
4
5
6
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(50) | NO   | PRI | NULL    |       |
| password | varchar(32) | NO   |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+

username.php
this is the html form and jQuery function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<html>
  <head>
       <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
      <!-- <script type="text/javascript" src="js/jquery-ui-1.8.17.custom.min.js"></script> -->
        
       <script type="text/javascript">
 
         $(document).ready(function(){
            $("#username").change(function(){
                 $("#message").html("<img src='ajax-loader.gif' /> checking...");
             
 
            var username=$("#username").val();
 
              $.ajax({
                    type:"post",
                    url:"check.php",
                    data:"username="+username,
                        success:function(data){
                        if(data==0){
                            $("#message").html("<img src='tick.png' /> Username available");
                        }
                        else{
                            $("#message").html("<img src='cross.png' /> Username already taken");
                        }
                    }
                 });
 
            });
 
         });
 
       </script>
  </head>
 
  <body>
 
       <table>
        <tr>
              <td>Username</td>
              <td>:</td>
              <td><input type="text" name="username" id="username"/><td>
                <td id="message"><td>
        </tr>
 
        <tr>
              <td>Password</td>
              <td>:</td>
              <td><input type="text" name="password" id="password" /><td>
        </tr>
       </table>
  </body>
</html>

check.php

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
 
  mysql_connect("localhost","root","");
  mysql_select_db("php_usernamelivecheck");
 
  $username=$_POST["username"];
  $query=mysql_query("SELECT * from user where username='$username' ");
 
  $find=mysql_num_rows($query);
 
  echo $find;
 
?>

Try to type some letters in the username text field and then change focus of the cursor on the password field. Checking is on the way
checking

If the username you wrote is not yet registered, the system will tell you that it is available to be used.
available

However if you pick up username that have been registered the system will tell you too
taken

6 thoughts on “Live Username Availability Checking using Ajax and jQuery

  1. Pingback: Jquery Ajax username validation not working | BlogoSfera

Leave a Reply