5

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.

Ann L.
  • 13,760
  • 5
  • 35
  • 66

1 Answers1

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();
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928