Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using jQuery' s datepicker and asp.net MVC4. The datepicker works in Firefox but in IE7 i get the message through the asp.net's validation that the field is not a date.

This is the code for the datepicker

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

This is my globalization setting in Web.config

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

E.g. in Firefox the date is shown as "19/03/2012" string and accepted by the asp.net's validation setup (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. Nullable object must have a value."

My viewModel uses a null-able DateTime that i cast to a non null-able DateTime in the controllers post action. This works in Firefox but in IE7 the value for the date from the viewModel is null. What is the problem?

like image 849
mrt181 Avatar asked Mar 19 '12 16:03

mrt181


People also ask

How do I fix my datepicker position?

To change the position of the jQuery UI Datepicker just modify . ui-datepicker in the css file. The size of the Datepicker can also be changed in this way, just adjust the font size.

How do I change datepicker format?

In the Data type Format dialog box, do one of the following: To format the control to show the date only, select the display style that you want in the Display the date like this list.

How datepicker is implemented in jQuery?

The datepicker is tied to a standard form input field. Focus on the input (click, or use the tab key) to open an interactive calendar in a small overlay. Choose a date, click elsewhere on the page (blur the input), or hit the Esc key to close. If a date is chosen, feedback is shown as the input's value.


2 Answers

The following line does nothing:

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

if you don't include the corresponding language file which is not included by default in the ASP.NET MVC 4 template.

You may try setting the format explicitly:

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

But this only concerns how the date should be formatted after selecting it in the datepicker. It has nothing to do with validation.

The client side validation is performed by the jquery.validate plugin which in turn uses either the browser currently configured culture (which might explain the discrepancies you are observing between FF and IE, for example one might be configured to use en-GB and the other en-US) or ISO dates.

You could override this custom validation and make it use your custom format to ensure that this will work 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;
        },
        ''
    );
}
like image 58
Darin Dimitrov Avatar answered Oct 12 '22 16:10

Darin Dimitrov


Jquery localization files are available at :

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

simply run :

Install-Package jQuery.UI.i18n and add a script reference to "Scripts/jquery-ui-i18n.js"

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

like image 3
amarnath chatterjee Avatar answered Oct 12 '22 15:10

amarnath chatterjee