- 2 years ago 239
Javascript Question

ASP.NET MVC: Disable Submit button if Client Validation passes

I have a simple MVC4 form with Client Validation enabled:

@{ Html.EnableClientValidation(); }
@{ Html.EnableUnobtrusiveJavaScript(); }
@using (Html.BeginForm("Search", "Search", FormMethod.Post, new { id = "frmMain" }))
@Html.AntiForgeryToken() .....
<input type="submit" value="Search" id="btnSubmit" />

When the user clicks the submit and all validation passes on the client, I wish to have the submit button disabled as per jQuery below. How do I know if the validation has passed client side in side my jQuery script?

<script type="text/javascript">

$(document).ready(function () {
$('input[type=submit]').click(function (e) {
$(this).attr('disabled', true);
$(this).attr('value', 'Working');

Answer Source

You can hook this code into the form validator. Just make sure it executes AFTER the jquery.validator.unobtrusive.

    $("#you-form-id").data("validator").settings.submitHandler = function(form) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download