Validate a username using Javascript and PHP ( AJAX )

Ajax Logo
Who doesn’t hate these long forms ? You fill each field in them and when you submit, it will return something like :
‘ SORRY BUT YOUR USERNAME IS ALREADY TAKEN ‘.

In this tutorial I m gonna show you how to check if a username is valid without leaving the page using ajax (Asynchronous Javascript And XML).
Before I start, check the final result to see if this what you are looking for.

Demo: click here
Download files: click here

Ready ?
Lets roll

First of all what is AJAX ?
Ajax, or AJAX, is a web development technique used for creating interactive web applications. The intent is to make web pages feel more responsive by exchanging small amounts of data with the server behind the scenes, so that the entire web page does not have to be reloaded each time the user requests a change. This is intended to increase the web page’s interactivity, speed, functionality, and usability.
Like DHTML, LAMP, and SPA, Ajax is not a technology in itself, but a term that refers to the use of a group of technologies.

Now lets see those files

  • _ajax.new.js: is the main file. Javascript is the glue that connect the server side language to the HTML page.
  • index.html: is the HTML page where the user sends its request and receives the answer
  • ajax-test.php: is the php file where we will make all the database operations or simple checkings like string lenght etc…

Popularity: 35% [?]

14 Responses to “Validate a username using Javascript and PHP ( AJAX )”


  1. 1 Daniel

    I couldn’t understand some parts of this article o.us poetry, but I guess I just need to check some more resources regarding this, because it sounds interesting.

  2. 2 Steve

    error in demo code

    ajax-text.php line 9

    $username== “joo” should read $username== “moo”

  3. 3 Peter

    When I’m testing this script it really won’t validate as it stops after writing “Validating username…”?
    Suggestions?

  4. 4 moo

    I do appreciate that my name (”moo”) is in the valid usernames list .

    A simple Guess! what does “xxx” refer to?
    :)

  5. 5 dinesh

    Thanks Its really a good thing! Good work tom. Well done

  6. 6 Jo

    nice work..

  7. 7 Davy

    for those who want the script to check a MySQL database ;) here is the modified scritp

    i’m new to php, mysql and ajax but i’m setting up a new website and wen i first tested this script, i could not get it to work using a database, but now after an half an our testing, i have managed to get is to work, and it works like a charm (at first sight)!

    oh, and to peter i would like to say that it is probable the php version , on php 4.4.7 it works fine on my wamp test server (original and modified).

    when using the standard unmodified version with php 5.2.5, i have the same error as you had…

    [CODE]
    = 3 ){
    $query=(”Select * from user where uname=’$username’”);
    $result= mysql_query($query);
    $num=mysql_num_rows($result);
    if ($num > 0) {//Username already exist
    print “Username already exist”;
    }else{
    print “Username is valid”;
    }}
    else{
    print “Username must be longer then 3 characters”;
    }
    break;
    default:
    //silence is golden
    endswitch;
    ?>
    [/CODE]

    enjoy,

  8. 8 M. Dubb

    Hey Davy

    Your code is neat, but gives me an error each time I want to use it:

    =============
    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in
    ==============

    Running:
    MySQL 5.1.23
    PHP 5.2.5
    Apache 2.2.6

    It doesn’t make sense that I get these errors.
    It should work

    Any ideas?

    Cheers

  9. 9 M. Dubb

    Fixed it.
    No idea what it was but it works now.

    Thank you for the idea.

  10. 10 hasan

    ok, look, first of; thanks for writing fairly clear tutorial in English language. I really appriciate of this style, which make lots of things clear in a little sentence.

    2nd tutriols are really nice with out errors if someone is using IE 7…. with firefox beta 3 has some problems. it also works pretty well in Safari for win and Opera for win.

    So, thanks again and carry on, it really helps me a lot. the people who have some idea about web development but learning for new comming standards for web……….

    c.u

  11. 11 Electrominds

    Thank you :) so simply and easier to understand than other scripts. I did it with database connection and it worked so fast! Thank you again ;)

  12. 12 hanu

    Its very useful beginners of Ajax Like me… thank u very much..

  13. 13 Luke70

    Very nice scritp, I’ll use it with MySQL.
    I’ll let you know.

  14. 14 ABC

    poor

  1. 1 Ajax login form (PHP & Javascript) - FARAWAY
  2. 2 Google Pagerank using Ajax at FARAWAY
  3. 3 Ajax login form (PHP & Javascript) « Tancheng’s Weblog

Leave a Reply