views:

70

answers:

2

Hi,

Sorry to bother everyone again. For some reason when I try to run the following with validateusername() nothing happens.

<script language="javascript">
//<----------------------------------+
//  Developed by Roshan Bhattarai and modified by Harry Rickards
//  Visit http://roshanbh.com.np for the origional version of this script and more.
//  This notice MUST stay intact for legal use
// -------------------------->
function validateusername()
{
    $("#username").blur(function()
    {
     //remove all the class add the messagebox classes and start fading
     $("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn(1000);
     //check the username exists or not from ajax
     $.post("usernamecheck.php",{ username:$(this).val() } ,function(data)
        {
       if(data=='no') //if username not avaiable
       {
        $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
      { 
        //add message and change the class of the box and start fading
        $(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900,1);
      });  
          }
       else
       {
        $("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
      { 
        //add message and change the class of the box and start fading
        $(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1); 
      });
       }

        });

    });
}
</script>

It's probably a simple mistake, but would somebody be able to point me in the right direction?

Thanks

+1  A: 

Have you tried to display the values while it is running? Also, using a Javascript debugger also typically helps.

monksy
Will do. Thanks for the advice.
hrickards
+1  A: 

The code have no syntax errors, but as you use event binding, have you tried to run it on page load? I believe you don't see results because corresponding input doesn't run your function on desired event.

$().ready(function(){
validateusername();
});

Also, use <script type="text/javascript">, language is deprecated.

Deniss Kozlovs
Yeah it runs fine on page load, but it needs to run after a user clicks out of #username. Using onClick for #password doesn't work, presumably because of what you're saying. Is there an onClickOut or something (google doesn't seem to help or I'm searching for the wrong queries)? Will change that. Thanks.
hrickards
I think I've fixed the problem you pointed out by using onBlur in username to run validateusername(). Shouldn't that fix it? It still doesn't seem to work though, and using steven's advice of using a javascript debugger I get validateusername is not defined. Even creating a blank function named function1 and using that comes up with the same error, so I must be calling the function wrong or something. Is this not how you do it?<input type="text" name="username" id="username" onBlur="functionname()">
hrickards
Write events in lower case - onblur="". But otherwise, it's correct.
Deniss Kozlovs
Thanks for all the advice. I think I've solved the original problem but stumbled across another one, so I'll look through my code again and if I can't find the problem soon open another question. Thanks!
hrickards