Pink Code
Pink Code

Reputation: 1834

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 ?

Upvotes: 1

Views: 8111

Answers (3)

Vivek Pradhan
Vivek Pradhan

Reputation: 4847

From what I understand, you want to show the element with id loading when the form submits without an error.

The official documentation says :

Callback for handling the actual submit when the form is valid. Gets the form as the only argument. Replaces the default submit. The right place to submit a form via Ajax after it is validated.

So you could overwrite the submithandler like so:

Your html (just the form element, the rest could remain the same) :

 <form id="login" method="POST" action="#"> //Don't need the onsubmit here

Your javascript could be:

 $("#login").validate({
    errorElement: "div",
    errorPlacement: function(error, element) {
    error.appendTo("div#content-alert").addClass('alert alert-danger');
    }, 
    submitHandler: function(form) {
      $('#loading').show();   //Show the required element here
      form.submit(); //Submit the form normally if needed
    },
    rules: {
        name: {
            required: true,
            minlength: 6,
            alpha: true,
        },
    },
    messages: {
        name: {
            required: "requied message",
            minlength: "6 character"
        },

    }
});

Hope it gets you started in the right direction.

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

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();
        }
    });

});

Upvotes: 2

Bhushan Kawadkar
Bhushan Kawadkar

Reputation: 28513

Try this : use .valid() method to decide whether to show loading message or not.

HTML :

<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="return showLoading();">
..... //input
</form>

jQuery ;

var form =  $("#login");

function showLoading()
{
  if(form.valid())
  {
   $('#loading').show();
   return true;
  }
  return false;
}

$(document).ready(function () {
    form.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"
            },

        }
    });

});

Upvotes: 0

Related Questions