Quang Vo
Quang Vo

Reputation: 492

Prevent multiple form submits in MVC 3 with validation

I have a form with many input fields on it, some with validation. I use the default validation plugin in MVC 3. If the server response is slow, the user may click to submit another time, which may cause undesired behavior. How to prevent this? I've tried injecting code in the .submit event of the form, but this event is raised even if the validation fails.

Upvotes: 7

Views: 1932

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You could check whether validation succeeded and disable the submit button:

$(function () {
    $('form').submit(function () {
        if ($(this).valid()) {
            $('input[type="submit"]').attr('disabled', 'disabled');
        }
    });
});

Upvotes: 19

Related Questions