2

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
  • 1
    Check if some scripts are missing. Commented Jul 8, 2013 at 11:53
  • 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. Commented Jul 8, 2013 at 11:55

2 Answers 2

7

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)
Sign up to request clarification or add additional context in comments.

Comments

5

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 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.