Peter
Peter

Reputation: 7804

jQuery.validate stops my form from being submitted

jQuery.validate stops my form from being submitted. I would like it to just show the user what is wrong but allow them to submit anyway.

I am using the jquery.validate.unobtrusive library that comes with ASP MVC.

I use jquery.tmpl to dynamically create the form and then I use jquery.datalink to link the input fields to a json object on the page. So my document ready call looks something like this.

jQuery(function ($) {
  // this allows be to rebind validation after the dynamic form has been created
  $("form").removeData("validator");
  $("form").removeData("unobtrusiveValidation");
  $.validator.unobtrusive.parse($("form"));

  // submit the answers
  $("form").submit(function(e) {
    $("input[name=jsonResponse]").val(JSON.stringify(answerArray));
    return true;             
  });
}

I note that there is an option

$("form").validate({ onsubmit: false });

but that seems to kill all validation.

So just to recap when my form is rendered I want to show all errors immediately but I don't want to prevent the submit from working.

Upvotes: 2

Views: 408

Answers (1)

Peter
Peter

Reputation: 7804

So after some research (reading the source code) I found I needed to do 2 things

add the class cancel to my submit button

<input id="submitButton" type="submit" class="cancel" value="OK" />

This stops the validation running on submit.

To validate the form on load I just had to add this to my document ready function

$("form").valid();

Hope this helps someone else

Upvotes: 2

Related Questions