6

I am disabling my submit buttons when submitting a form, for preventing the user to submit the form multiple times.

$(function ()
{
    $("form").submit(function ()
    {
        $(":submit", this).attr("disabled", "disabled");
    });
});

But if the client validation fails I want to enable the button again. I am using asp.net mvc unobtrusive client validation

Niels Hanberg
  • 221
  • 3
  • 7

2 Answers2

2

You can use the jQuery One event as detailed in this answer.

Most solutions to this issue revolve around testing $("form").valid() - you could probably use this in your function to determine whether to disable the submit button.

Community
  • 1
  • 1
Timbo
  • 4,505
  • 2
  • 26
  • 29
  • If it is the only form on the page, there should be e global boolean variable "Page_IsValid" you can use to determine if the button should be disabled. The client side validation API is explained here: http://msdn.microsoft.com/en-us/library/aa479045.aspx#aspplusvalid_clientside – geon Apr 06 '11 at 11:27
0

You have to check if there is any error before disabling the submit button

$('#myform').submit(function () {
    if ($(this).find('.input-validation-error').length == 0) {
        $(this).find(':submit').attr('disabled', 'disabled');
    }
});

Hope it helps!

VinnyG
  • 6,883
  • 7
  • 58
  • 76