admin管理员组

文章数量:1294324

Folks,

I have an MVC3 data entry form scenario that involves asking the user to confirm something after client-side validation has been satisfied, but before the post to the server.

Is there a way to insert some javascript into the sequence of events after the validation framework gives the go-ahead to post back, but before the post back happens?

(And of course if the user declines the confirmation, the post back has to cancel, too. )

Many thanks.

Folks,

I have an MVC3 data entry form scenario that involves asking the user to confirm something after client-side validation has been satisfied, but before the post to the server.

Is there a way to insert some javascript into the sequence of events after the validation framework gives the go-ahead to post back, but before the post back happens?

(And of course if the user declines the confirmation, the post back has to cancel, too. )

Many thanks.

Share asked Oct 21, 2011 at 21:57 Ann L.Ann L. 14k5 gold badges38 silver badges66 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

You could subscribe for the .submit event of the corresponding form and check if it is valid:

$(function() {
    $('form').submit(function() {
        if ($(this).valid()) {
            // client validation passed successfully
        } else {
            alert('there was an error during client validation');
            // cancel the submission of the form
            return false;
        }
    });
});

or if you don't want to subscribe for the submission of the form and you want to verify if client validation passes for a given form you could always check like this:

var isValid = $('#someFormId').valid();

本文标签: jqueryMVC 3 running javascript after validation has completed but before the actual postStack Overflow