58

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>
3

12 Answers 12

60

It seems like there is a lot of incorrect information about the ValidationSettings:UnobtrusiveValidationMode value. To Disable it you need to do the following.

<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />

The word None, not WebForms should be used to disable this feature.

4
  • 5
    I started with a new Internet MVC4 project and added N2 CMS. I was getting this error and this answer solved the problem for me. Thank you!
    – kingdango
    Feb 7, 2013 at 18:07
  • This solved my issue that arose when I upgraded my ASP.NET WebForms project created as .NET 4.0 to 4.5
    – sobelito
    May 13, 2013 at 17:08
  • exactly this one is right but this line will be in the web.config Oct 16, 2013 at 5:54
  • 3
    But now i am facing another issue it turned off my validation now hot to turn on the validationn Oct 16, 2013 at 5:59
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" />
2
  • Can you confirm that jQuery is loaded and maybe test using some dummy jquery code that it works as expected? It should ideally work provided your jquery file is found at ~/scripts/jquery-1.4.1.js
    – Jagmag
    Sep 17, 2012 at 4:19
  • Yes I can confirm it. I uploaded the code to my Github site. github.com/jupaol/LearningProjects/blob/master/VS%202012/src/…
    – Jupaol
    Sep 17, 2012 at 5:26
28

More Info on ValidationSettings:UnobtrusiveValidationMode

Specifies how ASP.NET globally enables the built-in validator controls to use unobtrusive JavaScript for client-side validation logic.

Type: UnobtrusiveValidationMode

Default value: None

Remarks: If this key value is set to "None" [default], the ASP.NET application will use the pre-4.5 behavior (JavaScript inline in the pages) for client-side validation logic. If this key value is set to "WebForms", ASP.NET uses HTML5 data-attributes and late bound JavaScript from an added script reference for client-side validation logic.

Example:

    <appSettings>
      <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
    </appSettings>
19

Other than the required "jquery" ScriptResourceDefinition in Global.asax (use your own paths):

    protected void Application_Start(object sender, EventArgs e)
    {            
        ScriptManager.ScriptResourceMapping.AddDefinition(
            "jquery",
            new ScriptResourceDefinition
            {
                Path = "/static/scripts/jquery-1.8.3.min.js",
                DebugPath = "/static/scripts/jquery-1.8.3.js",
                CdnPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js",
                CdnDebugPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.js",
                CdnSupportsSecureConnection = true,
                LoadSuccessExpression = "jQuery"
            });
    }

You additionally only need to explicitly add "WebUIValidation.js" after "jquery" ScriptReference in ScriptManager (the most important part):

       <asp:ScriptManager runat="server" EnableScriptGlobalization="True" EnableCdn="True">
            <Scripts>
                <asp:ScriptReference Name="jquery" />
                <asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" />
            </Scripts>
        </asp:ScriptManager>

If you add it before "jquery", or if you don't add any or both of them at all (ASP.Net will then automatically add it before "jquery") - the client validation will be completely broken:

http://connect.microsoft.com/VisualStudio/feedback/details/748064/unobtrusive-validation-breaks-with-a-script-manager-on-the-page

You don't need any of those NuGet packages at all, nor any additional ScriptReference (some of which are just duplicates, or even a completely unnecessary bloat - as they are added automatically by ASP.Net if needed) mentioned in your blog.

EDIT: you don't need to explicitly add "WebForms.js" as well (removed it from the example) - and if you do, its LoadSuccessExpression will be ignored for some reason

5

All the validator error has been solved by this

 <appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>

Error must be vanished enjoy....

3

Add a reference to Microsoft.JScript in your application in your web.config as below :

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5">
      <assemblies>
        <add assembly="Microsoft.JScript, Version=10.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
      </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <appSettings>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="none"/>
  </appSettings>
</configuration>
2
  • 1
    Why do you need Microsoft.JScript ? Doesn't make any sense, really!
    – DATEx2
    Dec 5, 2014 at 10:36
  • You set the ValidationSettings:UnobtrusiveValidationMode to none. That's what's making it work.
    – JJS
    Dec 5, 2014 at 21:09
2

Just copy & paste any JQuery file in your project and add a Global.asax file and modify it as below:

I just paste the JQuery file in my project and add a reference in Global.asax file:

protected void Application_Start(object sender, EventArgs e)
    {
        ScriptManager.ScriptResourceMapping.AddDefinition(
        "jquery",
        new ScriptResourceDefinition
        {
            Path = "~/jquery-1.10.2.js",
            DebugPath = "~/jquery-1.10.2.js",
            CdnPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.2.min.js",
            CdnDebugPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.2.js",
            CdnSupportsSecureConnection = true,
            LoadSuccessExpression = "jQuery"
        });
    }
1

In the Nuget Package manager search for AspNet.ScriptManager.jQuery instead of jquery. THat way you will not have to set the mappings yourself and the project will work with the click of a simple install.

Or disable the Unobtrusive Validation by adding this line to the Configuration tag of the web.config file in the project.

  <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
1

In "configuration file" instead this lines:

<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />

by this lines:

<compilation debug="true" targetFramework="4.0" />
<httpRuntime targetFramework="4.0" />

This error because in version 4.0 library belong to "asp:RequiredFieldValidator" exist but in version 4.5 library not exist so you need to add library by yourself

1
  • Pages worked fine yesterday, co-worker adds targetFramework="4.6.1" to web.config, everyone gets latest today and their pages with ASP.Net validators break. Three hours later, after reviewing jquery files and all the wrong places I realize this is framework dependent and see your answer. Thanks +1 Apr 26, 2018 at 21:17
0
<add key="ValidationSettings:UnobtrusiveValidationMode" value="WebForms" />

this line was not in my WebConfig so : I simple solved this by downgrading targetting .Net version to 4.0 :)

0

in visual studio 2012 in the web.config change the targetFramework=4.5 to targetFramework=4.0

-1

I suggest to instead this lines

<div>
    <asp:TextBox runat="server" ID="username" />
    <asp:RequiredFieldValidator ErrorMessage="The username is required" ControlToValidate="username" runat="server" Text=" - Required" />
</div>

by this line

<div>
    <asp:TextBox runat="server" ID="username" required />
</div>
1
  • This could be skipped by inspect element developer option of browsers. Add following line in 'Web.config' <appSettings> <add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/> </appSettings> Oct 7, 2016 at 5:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.