The jQuery validation plugin intercepts a submit form event with an event handler that calls the validate() function to validate() form before submitting, and thus prevents the form from submitting if validation fails.
If you hook up the submit event for each of your forms using jQuery and do it at the bottom of the form, it will add your own handler to execute before the plugin handler. Thus, you can call the valid() function on the form (s) yourself and run any custom code you want after a full form check. The plugin valid() function calls the validate() function on the form, and then returns a boolean for success / failure. If you don't care about success / failure, you can just call validate() directly and not call valid() .
When you call valid() or validate() on the form, it will continue to work with the unobtrusive validation of the MVC3 client to mark the form with validation errors, except that now you can do something else after the call.
I set breakpoints in my own submit() handler (below) and in the jQuery Validation validate() and valid() plugin functions. I see that they get called once with this implementation, so your validation for the full form should not be done multiple times.
Of course, the plugin also intercepts tricks, blur, keystrokes and event clicks, so the validators for individual fields are already called several times by design, so the user is provided with immediate feedback on fields with an error / allowed form. Validation is lazy because it does not run until the user enters a value in the field. When the user enters a value and leaves the field, this field is checked. If the field is marked invalid, a re-check is performed because the invalid field is checked repeatedly at various events until it is recognized as valid.
At the bottom of the page, place the tags below the form . As written, with the $("form") selector, it handles the submit event for all form elements on the page. Of course, you can use the jQuery selector to select specific form tags for validation. Just call valid() or validate() on the form whenever you want to perform validation, and follow it with any code that you want to execute once.
To get the test result:
<form> ... </form> <script type="text/javascript"> $("form").submit(function (e) { if ($(this).valid() == false) { alert("yo!"); </script>
Or, if you do not want to know the result of the check:
<script type="text/javascript"> $("form").submit(function (e) { $(this).validate(); alert("yo!"); </script>