Alex Rivera | Logout

ASP.Net 2012 Unobtrusive Validation with jQuery

Asked 2012-09-17T01:18:58.777
59

I was playing with Visual Studio 2012 and I created an empty ASP.Net Web Application, when I tried to add the traditional validator controls to a new page, this error occurs:

WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive).

What are the steps to fix it?

This is my page markup:

<asp:Panel runat="server" ID="pnlUsername" GroupingText="Username settings">
    <div>
        <asp:Label ID="usernameLabel" Text="Please enter your username" runat="server" AssociatedControlID="username" />
    </div>
    <div>
        <asp:TextBox runat="server" ID="username" />
        <asp:RequiredFieldValidator ErrorMessage="The username is required" ControlToValidate="username" runat="server" Text=" - Required" />
    </div>
</asp:Panel>
Edit
Report

1 Answer

32

This is the official Microsoft answer from the MS Connect forums. I am copying the relevant text below :-

When targeting .NET 4.5 Unobtrusive Validation is enabled by default. You need to have jQuery in your project and have something like this in Global.asax to register jQuery properly:

ScriptManager.ScriptResourceMapping.AddDefinition("jquery", 
    new ScriptResourceDefinition {
        Path = "~/scripts/jquery-1.4.1.min.js",
        DebugPath = "~/scripts/jquery-1.4.1.js",
        CdnPath = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1.min.js",
        CdnDebugPath = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1.js"
    });

Replacing the version of jQuery with the version you are using.

You can also disable this new feature in web.config by removing the following line:

<add key="ValidationSettings:UnobtrusiveValidationMode" value="WebForms" />
answered 2012-09-17T03:30:00.523

Your Answer