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

iLemming picture iLemming · May 24, 2011 · Viewed 15.3k times · Source

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.

Answer

iLemming picture iLemming · May 24, 2011

Oh...

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