Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 548
Quang Vo Avatar asked May 21 '11 07:05

Quang Vo


People also ask

How do I stop a form from submitting multiple times?

Use JQuery to Prevent Multiple Form Submissions To prevent the user from submitting a form multiple times, we'll use JQuery to listen for the submission event. Once the form is submitted, we'll add a disabled attribute to the button to prevent multiple form submissions. Now the user can't click it again.

How Stop form submit in MVC?

preventDefault() method used here stops the default action of submit button from submitting the form.

How can we prevent resubmission of a form in asp net MVC?

After Form submission, when the Refresh Button in the Browser is clicked or the F5 key is pressed, a warning popup comes up which warns against Form resubmission. The solution is very simple, either the page must be redirected to itself or to some other page in order to avoid this particular behavior.

How do you restrict action methods?

To restrict the public action method in MVC, we can use the “NonAction” attribute. The “NonAction” attribute exists in the “System. Web. MVC” namespace.


1 Answers

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

$(function () {
    $('form').submit(function () {
        if ($(this).valid()) {
            $('input[type="submit"]').attr('disabled', 'disabled');
        }
    });
});
like image 88
Darin Dimitrov Avatar answered Dec 28 '22 15:12

Darin Dimitrov