In a previous MVC 2.0 application, I included (in my Site.Master) a reference to the CDN file of jquery.validate.min.js
for all my client-side validation.
I’m now creating an MVC 3.0 project and I’m a bit confused with the amount of articles written about “how to use client-side validation using MicrosoftMvcValidation.js”.
In most (if not all) articles, when I see a reference to MicrosoftMvcValidation.js
I also see a reference to jquery.validate.js
which is kind of confusing (considering, in the past, I’ve only used bassistance’s file).
So my questions are:
Scripts
” folder, by default, it looks like both files
are there but why? Do they give us the choice or do they work hand in
hand?Any help shedding some light on this would be great!
Thanks in advance!
MVC3 supports two modes of client-side validation:
Classical validation using MicrosoftMvcValidation.js
To enable this, call Html.EnableClientValidation()
before Html.BeginForm()
.
Unobtrusive validation using jquery.validate.js and jquery.validate.unobtrusive.js (new to MVC3)
To enable this, add the following settinsg to Web.config:
<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
MVC2 provided the choice between Microsoft's own client-side validation, or jQuery validation.
MVC3 continues this, but only provides unobtrusive validation (i.e. new HTML5 validation features) if you go down the jQuery route.
In both cases, the prevailing opinion seems to be that jQuery is the better choice, as it is more easily extended and simpler to use. There is no need to include both scripts in either your project or your webpage - typically, you would choose one and delete the other.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With