Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a script after client validation in asp.net mvc

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

like image 943
Niels Hanberg Avatar asked Apr 06 '11 09:04

Niels Hanberg


People also ask

How does MVC handle client-side validation?

Firstly, you just need to create an ASP.NET MVC application. To create a new ASP.NET MVC application, Open Visual Studio choose File, New, then Project. It will open a New Project window, from where you need to choose node Visual C# and then Web and from the right pane you need to choose ASP.NET Web Application.

How do I make sure client validation is enabled in MVC?

It turns out that to enable client side validation without using the HtmlHelpers and a model you just have to add an input with data-val="true" and then data-val- followed by validation method that you want to apply (e.g. data-val-required ), the value of which will be the error message presented to the user (e.g. data ...

How ModelState IsValid works in MVC?

ModelState. IsValid indicates if it was possible to bind the incoming values from the request to the model correctly and whether any explicitly specified validation rules were broken during the model binding process. In your example, the model that is being bound is of class type Encaissement .


1 Answers

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.

like image 196
Timbo Avatar answered Oct 23 '22 05:10

Timbo