jQuery Date Validation in Chrome

the date validator in jQuery Validate is broken if you want to use UK format dates with the jQuery datepicker; it tries to validate as US format irrespective of the format you tell it to use. And because webkit correctly barfs on invalid dates like that, you can’t submit.

Found a useful post: jQuery Date Validation in Chrome.

Chrome seems to ignore the current locale when it creates the date object, so a valid date in UK dd/mm/yy format – 29/10/2009 – fails as Chrome seems to interpret it in US mm/dd/yy format. Easy to test outside of all other code by entering some simple JavaScript into the address bar:

javascript:new Date(’29/10/2009′).toString()

Which Firefox renders as expected

I take issue with the last bit: Firefox renders that date as May 10, 2012 instead of October 29, 2012 – hardly what I’d have expected…

So the upshot is to make it work in Chrome or Safari you need to paste in a function to patch jQuery. Bloody annoying

date: function(value, element) {
     //ES - Chrome does not use the locale when new Date objects instantiated:
     //return this.optional(element) || !/Invalid|NaN/.test(new Date(value));
     var d = new Date();
     return this.optional(element) || !/Invalid|NaN/.test(new Date(d.toLocaleDateString(value)));
},
Advertisements