6

I have an app that I have recently upgraded to .Net 4.5.1 and MVC 4. I am using the jQuery datepicker and jQuery.validation 1.11.1.

I am in the UK therefore the dates will be in the en-GB locale ("dd/mm/yyyy"). I have tried what is suggested here, here and here but to no avail.

I also have in my web.config:

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

and have set the globalisation in IIS to en-GB, but every date that is input is validated as a US format date.

Can anyone help please?

2
  • I had the same problem a while a ago, couldn't find an apt solution for the issue, as a work around, I disabled the MVC's Client validation, wrote my own custom validation for required and set the input date field as readonly so users can't modify the date provided using datepicker Commented Oct 22, 2013 at 13:46
  • When you apply the solution above this problem will occure: stackoverflow.com/questions/26279779/… Commented Oct 10, 2014 at 13:19

3 Answers 3

8

Changing the date validation method in jQuery.validate.js to the follwing solved the issue:

date: function (value, element) {
        $.culture = Globalize.culture("en-GB");
        var date = Globalize.parseDate(value, "dd/MM/yyyy", "en-GB");
        return this.optional(element) || 
                       !/Invalid|NaN/.test(new Date(date).toString());
    }

Tested in Chrome, FF and IE

Sign up to request clarification or add additional context in comments.

4 Comments

In the jQuery.validate.js file in your solution
And Do i need To add Globalize.js because I replaced the Old date: function ... with this one but it didn't worked Always I'm getting The filed ... must be Date
Yes I am also facing same problem though I had done as said... Any suggestions?
I also get error after implementing this that its looking for Globalize.js, not sure what that is or where to put it
0

in The Jquery which is calling simply add the format.

$(function() { $("#datepicker").datepicker({dateFormat: 'dd/mm/yy'}); });

http://www.matthewcarlin.co.uk/jquery-quick-tip-2-changing-date-format

1 Comment

Doesn't answer the question. They are asking to validate the date produced from the datepicker.
0

Having just had to figure this out, I overrode the rule and used a data attribute

if (typeof jQuery.validator.methods.date !== 'function') {
    var f = function (value, element) {
        var e = $(element).attr('data-dateformat');
        if (e && (e.length)) {
            var dt = jQuery.datepicker.parseDate(e, value);
            return this.optional(element) || !/Invalid|NaN/.test(dt.toString());
        } else {
            return this.optional(element) || !/Invalid|NaN/.test(new Date(value).toString());
        }
    }
    jQuery.validator.addMethod("date", f);
} else {
    var m = jQuery.validator.methods.date;
    var f = function(value, element) {
        var e = $(element).attr('data-dateformat');
        if (e && (e.length)) {
            var dt = jQuery.datepicker.parseDate(e, value);
            return this.optional(element) || !/Invalid|NaN/.test(dt.toString());
        } else {
            return m(value, element);
        }
    }
    jQuery.validator.addMethod("date", f);
}

It ASSUMES you have datepicker, but if you are using dates, and jQuery, I think that's a safe bet. If the element has a date format defined in data-dateformat it will parse it using that, otherwise it will fall back to the default date rule, and if that wasn't present it implements the default rule.

I placed this in my site global code file to keep it outside the jQuery scripts

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.