How to fix regional settings for jQuery datepicker so that it works in Firefox and IE7?

I am using jQuery datepicker and asp.net MVC4. Datpicker works in Firefox, but in IE7 I get a message through checking asp.net that the field is not a date.

This is the code for datepicker

if (!Modernizr.inputtypes.date) { $(function() { $.datepicker.setDefaults($.datepicker.regional['en-GB']); $(".datefield").datepicker(); }); } 

This is my globalization option in Web.config

<globalization uiCulture="en-GB" culture="en-GB" />

eg. in Firefox, the date is displayed as the string "03/19/2012" and is accepted by setting the asp.net check (on the client and server side). In IE7, the same date string is not accepted on the client. If I change it to "03/19/2012", the client accepts the date, but then the server throws an exception - "InvalidOperationException. The Nullable object must be set to".

My viewModel uses a null DateTime value, which I passed to a DateTime value that does not have null values ​​as a result of controller add-ons. This works in Firefox, but in IE7 the date value from viewModel is null. What is the problem?

+6
source share
2 answers

The following line does nothing:

 $.datepicker.setDefaults($.datepicker.regional['en-GB']); 

if you did not specify the appropriate language file , which by default is not included in the ASP.NET MVC 4 template.

You can try to explicitly set the format:

 $.datepicker.setDefaults({ dateFormat: 'dd/mm/yy' }); 

But this only applies to how the date should be formatted after it is selected in the datepicker. This has nothing to do with verification.

Client-side validation is performed by the jquery.validate plugin, which, in turn, uses the currently configured culture (which can explain the inconsistencies that you observe between FF and IE, for example, you can configure to use en-GB and other en-US ) or ISO date.

You can override this custom check and use it in your custom format to ensure that it will work in a cross browser:

 if (!Modernizr.inputtypes.date) { $(function () { $.datepicker.setDefaults({ dateFormat: 'dd/mm/yy' }); $('.datefield').datepicker(); }); jQuery.validator.addMethod( 'date', function (value, element, params) { if (this.optional(element)) { return true; }; var result = false; try { $.datepicker.parseDate('dd/mm/yy', value); result = true; } catch (err) { result = false; } return result; }, '' ); } 
+13
source

Jquery localization files are available at:

http://nuget.org/packages/jQuery.UI.i18n

just run:

Install jQuery.UI.i18n package and add script link to "Scripts / jquery-ui-i18n.js"

from the package manager console, then this will work:. (. $ Datepicker.regional ['en-GB']) $ datepicker.setDefaults;

+2
source

Source: https://habr.com/ru/post/911113/


All Articles