Search code examples
validationasp.net-mvc-3jqueryunobtrusive-validation

How to force form client-side validation in or before $.ajax()


I have a form and unobtrusive validations are enabled. By default in submit method client side validation gets triggered and (if you have any errors) the form looks like this:

enter image description here

The validation happens even before any data gets sent to the server.

Now this behavior doesn't work if you want to use $.ajax method. Client side validation doesn't work. You have to manually check all the fields in your javascript, losing all the beauty of DataAnnotations.

Is there any better solution? I could've use jquery's submit() but I guess it doesn't have callback like $.ajax.


Solution

  • Oh...

    if (form.valid()) // do submit