Is it possible to โ€œcrackโ€ the unobtrusive validate supported in ASP.NET MVC 3?

I want to be able to use the built-in, annotation-based, unobtrusive client-side validation, but make my own ajax presentation form after I know that it passes.

Something like this jQuery bit:

$('#form').submit(function(e) {
  e.preventDefault();

  if (PassesUnobtrusiveValidation()) {
    // ajax submit form
  }
});

Is there an event PassedValidationor something like that (built into the standard, based on asp.net mvc 3 data-based validation based on data-based annotation) Can I connect (e.g. psuedocode in the example)?

I want to do this, so I can still use validation based on data annotation and then submit the form asynchronously as I want. I want to avoid writing a general check in the client, letting asp.net mvc 3 take care of this for me, and then submit the form as I want using$.ajax();

+3
source share
1 answer

If you use jquery.validate:

$('#myForm').submit(function() {
    if ($(this).valid()) {
        // Client side validation passed => submit the form
    }
});

Another possibility is to connect the options plugin :

$.validator.setDefaults({
    submitHandler: function(form) {
        // Client side validation passed => submit the form
    }
});

If you are using MSAjax, then good luck with that.

+6
source

All Articles