Leron
Leron

Reputation: 9856

Client side validation in MVC 4 with jQuery 1.10.2

I am building an ASP.NET MVC 4 based application and try to follow the code from the standard internet template where's possible.

Yesterday I started to implement my forms and noticed that I don't get a client side validation and the content is always submitted.

I render the form like this:

@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

        <ul>
            <li>
                @Html.LabelFor(m => m.Title)
                <br />
                @Html.TextBoxFor(m => m.Title)
                <br />
                @Html.ValidationMessageFor(m => m.Title)
            </li>
            <li>
            //more list items here

and because I didn't start with the debugging I render the scripts as follows:

@Scripts.Render("~/Scripts/jquery-1.10.2.min.js")
@Scripts.Render("~/Scripts/jquery.validate.min.js")
@Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js")
@Scripts.Render("~/Scripts/jquery.validate.unobtrusive.min.js")

Then, in firebug, when I submit an incorrect form I get the following error:

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

And then I saw this - Also bear in mind that in jQuery 1.9 the .live() method has been removed which was one of the breaking changes an another SO question here. So I decided to try and changed the jQuery version to :

@Scripts.Render("~/Scripts/jquery-1.7.1.min.js")

and the validation started to work normally.

Some further research showed me that most some people made their custom jQuery.val* scripts in order to make it work with a new version of jQuery.

My question is - is there a standard packaged/bundle for client side validation that I can add to my project and use with newer version of jQuery (the one I'm using now is 1.10.2)? Do I have to use jQuery 1.7.* for my client validation parts or now there is something else supported by MS (like validation using knockout.js which is part of the standard template) or maybe even other js library that is now used for client side validation in MVC projects?

Upvotes: 2

Views: 1165

Answers (1)

Carl Raymond
Carl Raymond

Reputation: 4479

I experienced similar jQuery validation problem with 1.10.2 (I would get "Uncaught SyntaxError: Unexpected token u"). I added the jQuery.Migrate package, and it fixed it. I believe the error comes from a change in parseJSON.

Upvotes: 2

Related Questions