2
votes

I'm using ASP MVC 4 and I have both ClientValidationEnabled and UnobtrusiveJavaScriptEnabled appSettings turn on but when I submit a form with unobtrusive validation data attributes it get submitted even when it is invalid. The validation message appear but the form still get submitted.

JavaScript is enabled in my browser off course.

2
Check if some scripts are missing. - Adriano Silva
without more info it is very hard helping you. check you don't have another javascript submit of the form, you don't have javascript library collisions or as @AdrianoSilva says you are not missing a script. - Alexandros B

2 Answers

7
votes

Make sure, you have following libraries linked

<script src="~/Scripts/jquery.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>

Check your configuration. These two should look as follows:

<appSettings>
    <add key="ClientValidationEnabled" value="true"/> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/> 
</appSettings>

Don't forget to add:

@Html.ValidationMessageFor(m => m.FieldToValidate)
5
votes

For MVC4 in your master page add below line

@Scripts.Render("~/bundles/jqueryval")

and make sure that in bundles.config below lines are present.

bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.unobtrusive*",
                    "~/Scripts/jquery.validate*"));

once you have added jquery.unobtrusive you will encounter below error.

the live() function is not available anymore in jquery 1.9, which has been deprecated.

TypeError: $(…).live is not a function

in order to resolve this you can manyally replace .live with .On or just add beta release from nuget

Install-Package Microsoft.jQuery.Unobtrusive.Ajax -Pre