How to disable form submit if the username is not available

Posted by Rajasekar on Stack Overflow See other posts from Stack Overflow or by Rajasekar
Published on 2010-05-19T10:42:48Z Indexed on 2010/05/19 11:00 UTC
Read the original article Hit count: 228

Filed under:
|
|
|

I have checked the user name availability. The problem is, even if the username is not available, the form is posting.

Edited Code:

<SCRIPT type="text/javascript">
$(document).ready(function(){

$("#emailch").change(function() { 

    var usr = $("#emailch").val();
    if(usr.length >= 3)
    {
        $("#status").html('<img src="images/loader.gif" align="absmiddle">&nbsp;Checking availability...');
        $.ajax({  
            type: "POST",  
            url: "includes/check.php",  
            data: "username="+ usr,  
            success: function(msg){   
                $("#status").ajaxComplete(function(event, request, settings){ 
                    if(msg == 'OK')
                    { 
                        $("#username").removeClass('object_error'); // if necessary
                        $("#username").addClass("object_ok");
                        $(this).html('&nbsp;<img src="images/tick.gif" align="absmiddle">');

                    }
                    else
                    {
                        $("#username").removeClass('object_ok'); // if necessary
                        $("#username").addClass("object_error");
                        $(this).html(msg);


                    }

                });

            }
        }); 
    }

    else
    {
        $("#status").html('<font color="red">The username should have at least <strong>3</strong> characters.</font>');
        $("#username").removeClass('object_ok'); // if necessary
        $("#username").addClass("object_error");
    }

});

});
</SCRIPT>

What I want is that, the form should not be posted if the user name is not available. I have tried by using return false; in IF CONDITION but it fails. Please suggest any alternate method. Any help will be appreciated.

© Stack Overflow or respective owner

Related posts about AJAX

Related posts about JavaScript