ASP.Net 2012 Unobtrusive Validation Using jQuery

I played with Visual Studio 2012, and I created an empty ASP.Net web application , when I tried to add traditional validation elements to a new page, this error occurs:

WebForms UnobtrusiveValidationMode requires ScriptResourceMapping for 'jquery'. Add a ScriptResourceMapping named jquery (case sensitive).

What are the steps to fix it?

This is my page layout:

<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> 
+52
Sep 17 '12 at 1:18
source share
12 answers

There seems to be a lot of incorrect information about the value of ValidationSettings: UnobtrusiveValidationMode. To Disable , you need to do the following.

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

To disable this feature, use the word None, not WebForms.

+54
Feb 05 '13 at 23:06
source share

Additional information about ValidationSettings: UnobtrusiveValidationMode

Indicates how ASP.NET globally allows built-in validation elements to use unobtrusive JavaScript for client-side validation logic.

Type: UnobtrusiveValidationMode

Default Value: None

Note: If this key value is set to No [default], ASP.NET will use the pre-4.5 behavior (built-in JavaScript on the pages) for client-logical validation. If this key value is set to "WebForms" , ASP.NET uses HTML5 data attributes and late JavaScript with an added script link for client-side validation logic.

Example:

  <appSettings> <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" /> </appSettings> 
+25
Apr 05 '13 at 4:38
source share

Except for 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" }); } 

In addition, you only need to explicitly add "WebUIValidation.js" after the "jquery" ScriptReference in the 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 either or both of them at all ( ASP.Net will automatically add it before jquery), the client check will be completely violated: / p>

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

You do not need any of these NuGet packages at all, nor additional ScriptReference (some of them are just duplicates or even completely unnecessary bells and whistles), since they are automatically added by ASP.Net if necessary) on your blog.

EDIT: you do not need to explicitly add "WebForms.js" (removed from the example) - and if you do, its LoadSuccessExpression will be ignored for any reason

+15
Jul 22
source share

Add a link to Microsoft.JScript in your application in web.config , as shown 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> 
+5
Dec 02 '13 at 16:41
source share

All validation error was resolved by this

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

The error should go away, enjoy ....

+5
Apr 28 '15 at 21:08
source share

Just copy and paste any jQuery file into your project and add the Global.asax file and modify it as shown below:

I simply paste the jQuery file into my project and add the link to the 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" }); } 
+2
Dec 11 '13 at 5:58
source share

In Nuget Package Manager, find AspNet.ScriptManager.jQuery instead of jquery. Thus, you will not need to install the mappings yourself, and the project will work with the click of a simple installation.

Or turn off the unobtrusive check by adding this line to the Configuration tag of the web.config file in the project.

  <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" /> 
+1
Feb 20 '15 at 13:07
source share
 <add key="ValidationSettings:UnobtrusiveValidationMode" value="WebForms" /> 

this line was not in my WebConfig like this: I just solved this by lowering the target .Net version to 4.0 :)

0
Nov 16 '12 at 11:30
source share

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

0
Sep 01 '13 at 2:34 on
source share

I suggest strings instead

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

this line

 <div> <asp:TextBox runat="server" ID="username" required /> </div> 
0
Aug 28 '16 at 10:27
source share

In the "configuration file" instead of this line:

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

on these lines:

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

This error, because in the library version 4.0 belongs to "asp: RequiredFieldValidator", but in version 4.5 the library does not exist, so you need to add the library yourself

0
Aug 28 '16 at 11:01
source share



All Articles