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.
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.
preventDefault() method used here stops the default action of submit button from submitting the form.
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.
To restrict the public action method in MVC, we can use the “NonAction” attribute. The “NonAction” attribute exists in the “System. Web. MVC” namespace.
You could check whether validation succeeded and disable the submit button:
$(function () {
$('form').submit(function () {
if ($(this).valid()) {
$('input[type="submit"]').attr('disabled', 'disabled');
}
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With