Q:

jquery show/hide div after form validation

I show loading message after form submit like this:
<div id="loading" style="display:none;"><img class='image-widthauto' src="/img/Progressbar.gif" /></div>
<div id="content-alert"></div>
<form id="login" method="POST" action="" onsubmit="$('#loading').show();">
..... //input
</form>

i validate my form using jquery validation plugin:
$(document).ready(function () {
    $("#login").validate({
        errorElement: "div",
        errorPlacement: function(error, element) {
        error.appendTo("div#content-alert").addClass('alert alert-danger');
        }, 
        rules: {
            name: {
                required: true,
                minlength: 6,
                alpha: true,
            },
        },
        messages: {
            name: {
                required: "requied message",
                minlength: "6 character"
            },

        }
    });

});

Now, in form submit i see loading message and error box of validation(submit empty form value). i need to show loading message when my form not have a error using validation plugin.
how do fix this ?

A:

Instead of using onsubmit attribute, use the submitHandler callback of the validator
$(document).ready(function () {
    $("#login").validate({
        errorElement: "div",
        errorPlacement: function (error, element) {
            error.appendTo("div#content-alert").addClass('alert alert-danger');
        },
        rules: {
            name: {
                required: true,
                minlength: 6,
                alpha: true,
            },
        },
        messages: {
            name: {
                required: "requied message",
                minlength: "6 character"
            },

        },
        submitHandler: function (form) {
            $('#loading').show();
            form.submit();
        }
    });

});

