Validate a username using Javascript and PHP ( AJAX )

index.html:
First lets call our Ajax class. Here we have 2 functions:
hand(): this function will handle the response received and will write it .
validateUsername(): this function will actually launch the ajax class and call the method doGet.

1
2
3
4
5
6
7
8
9
10
11
var hand = function(str){
   window.document.getElementById('response_span').innerHTML=str;
}
function validateUsername(user){
  var strDomain='';
  window.document.getElementById('response_span').innerHTML=
"Validating username...";
  var ajax = new Ajax();
  ajax.doGet(strDomain+'ajax-test.php?action=validateUsername&
username='+user,hand,'text');
}

The HTML:

 <table>
   <tr>
     <th valign="top">Choose your username</th>
     <td><input type="text" name="username" value="" maxlength="50"
 size="30" onchange="validateUsername(this.value)"/><br />
	 <span id="response_span" class="smallalert"></span><br />
     </td>
   </tr>
  </table>

Popularity: 36% [?]

16 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

  15. 15 eric

    I keep getting Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource any one have any idea why?

    its refering to this area
    $query=(”Select * from inv_user where userid=’$username’”);
    $result = mysql_query($query);
    $num = mysql_num_row($result);
    if ($num > 0) {//Username already exist

  16. 16 Hi eric

    You’re getting this error because when you copy ad paste the code above it changes qutation mark into another completely defferent one. Simple change any double qutations into the one you have in your keyboard.
    It works fine with me and I’ve developed it according to my needs. The only problem which this code doesn’t deal with is the Middle East charsets!! I’m trying to integrate it with such charsets and if anyone has any idea how to solve this, I would be more than appreciated.

    Regards,

  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