navig8tr
navig8tr

Reputation: 1844

unobtrusive validation not triggering when using submit() function

In my view I have a button that submits the form:

<input type="button" value="Save Only" id="save" onclick="submitForm('SaveOnly')" />

This is one of a few different buttons, all of which will be handled differently in different Controller Action methods.

Also in the view is this submitForm(action) Javascript method:

function submitForm(action) {
    var form = document.getElementById("myForm");
    form.action = "/Area/MyController/" + action;
    form.submit();
}

When I use a submit button (rather than type="button"), unobtrusive client-side validation works properly. However, when I use this submitForm(action) function, the client-side validation does not happen. How can trigger the client side validation?

Upvotes: 1

Views: 3294

Answers (1)

hutchonoid
hutchonoid

Reputation: 33306

You can call it manually as follows:

function submitForm(action) {
    var $form = $("#myForm");
    $form.action = "/Area/MyController/" + action;

    $.validator.unobtrusive.parse($form);
    $form.validate();

    if ($form.valid()) {
       $form.submit();
    }
}

Upvotes: 2

Related Questions