Geeks With Blogs

News
Elton Stoneman (@EltonStoneman) IT Consultant, integration specialist, @Microsoft MVP and @Pluralsight author.

[Source: http://geekswithblogs.net/EltonStoneman]

We had a fiddly issue with date validation in an ASP.NET MVC page failing for a valid date in Chrome, but passing in Firefox, IE etc. Tracing through our own code and xVal, the issue was narrowed down to the jQuery validation plugin (jquery.validate.js). For simple date validation, the library instantiates a date object from the given text value and lets JavaScript raise errors for invalid dates:

    date: function(value, element) {

     return this.optional(element) || !/Invalid|NaN/.test(new Date(value));

    },

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:

- as does IE (even IE6):

- but which Chrome renders as an invalid date:

You can also test it by navigating to the jQuery date validation sample page, and checking results for the same date in different browsers.

The fix is a simple change to the jQuery script to force the use of the current locale:

    // http://docs.jquery.com/Plugins/Validation/Methods/date

    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)));

    },

 

Posted on Thursday, October 29, 2009 2:20 PM Annoying problems , Code Snippet | Back to top


Comments on this post: jQuery Date Validation in Chrome

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Thanks for the simple yet useful tutorial!
Left by Mark@School grants on Mar 05, 2010 6:26 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Thanks for this informative entry.I am forever searching for info on particular subjects.Thanks for the share!i like the details provided here.Keep it up.
Left by dedicated server-47 on Mar 27, 2010 10:20 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Hi,this is one of the finest site and I really appreciate for this.I’m going to bookmark this page so I can return and keep reading.
Left by Bulk SMS on Apr 06, 2010 6:52 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
thank u very much!
Left by steven on Apr 15, 2010 1:02 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Thank you very very much.
Left by More On Me on Apr 24, 2010 1:12 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Very interesting topic!I was very pleased to find this site.Will bookmark your site to check if you write more about in the future.
Left by nutritionals_74 on Apr 28, 2010 2:42 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Exactly what i was looking for.
Thanks for this informative entry.
Left by Planos de Hospedagem on May 10, 2010 2:51 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
First You got a great blog which i like a lot.Each & every tips of your post are awesome.Thanks a lot.Keep blogging.
Left by neways75 on May 24, 2010 7:49 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
These articles are greatly appreciated.I agree with most of what you are saying here.Thanks a lot for sharing.Keep up the good works.
Left by platinum protection85 on Jun 11, 2010 7:41 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
I really like this article and you are given here really a wonderful information.I tried that & its really worked….
Left by nutritionals-59 on Jun 14, 2010 5:36 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Nice post having excellent contents.I agree with most of what you are saying here.Thank you so much for sharing.Keep blogging.
Left by web host44 on Jul 09, 2010 6:22 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
very nice tuto
thank you
Left by fadi on Jul 13, 2010 8:13 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
This is first time I learn about this topic.This is really very nice one and gives in depth information. thanks for this nice article Good post…
Left by Ecommerce web design on Aug 06, 2010 6:52 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Great post.It’s really helpful for me which I have ever seen.I am very happy with your post.Thanks a lot for sharing.Keep up the good works.
Left by Sewage Pumps on Aug 25, 2010 10:06 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
This doesn't work as toLocaleDateString() doesn't accept parameters.

Anyone who uses this should probably check their forms as it brakes the date validation function i.e. it doesn't validate dates anymore.
Left by Sean Tomlins on Aug 31, 2010 11:36 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
If you are creating the date string without the Date object then you are better off creating your own validation method like this:

// This is when I expect a date string like dd/MM/yyyy
$.validator.addMethod("MyDateMethod", function (value, element) {
//ES - Chrome does not use the locale when new Date object is instantiated:
var myarray = value.split('/');
if (myarray.length != 3) {
return false;
}
var year = parseInt(myarray[2]);
var month = parseInt(myarray[1]);
var day = parseInt(myarray[0]);
return this.optional(element) || !/Invalid|NaN/.test(new Date(year,month,day));
}, "Please enter a Valid Date");

And then just append that method to an ID or Class
Left by Sean Tomlins on Sep 01, 2010 12:45 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Seems to be good only with chrome, not IE, non FF...
Left by Mad on Sep 21, 2010 12:18 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Same issue with Safari.
Here's the crossbrowser solution (dateITA):
http://ajax.microsoft.com/ajax/jquery.validate/1.7/additional-methods.js
Left by Mad on Sep 23, 2010 8:40 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
I wanted to thank you for this great read! Definitely enjoying every bit of it I've bookmarked to check out new things to post Many thanks for this great post! I've been looking for this for a long time
Left by email marketing services on Oct 01, 2010 12:32 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work.
Left by airport parking on Nov 19, 2010 7:03 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Thank you for the sensible critique. Me and my neighbor were just preparing to do some research about this. We got a grab a book from our local library but I think I learned more from this post. I am very glad to see such great information being shared freely out there.
Left by business to business directories on Nov 23, 2010 5:17 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
This is my first time i visit here. I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! Keep up the excellent.Thank you
Left by heating systems monroe mi on Nov 29, 2010 9:15 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Really impressed! Everything is very open and very clear explanation of issues. It contains truly information. Your website is very useful. Thanks for sharing. Looking forward to more!
Left by accent reduction training on Nov 29, 2010 9:55 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
while the blog post is easy to follow, the "FIX" doesn't work.
toLocaleDateString method takes NO parameters:

var d = new Date();
return this.optional(element) || !/Invalid|NaN/.test(new Date(d.toLocaleDateString(value)));

the code you posted absolutely ignores the `value` argument and always validates today's date. In fact your code should always return that the validated element's value is valid.
Left by this doesn't work on Dec 08, 2010 10:37 AM

# Craft show listings
Requesting Gravatar...
This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article

Left by Craftshow on Jan 02, 2011 5:21 PM

# Stefanie Mathew
Requesting Gravatar...
I suggest this site to my friends so it could be useful & informative for them also. Great effort. Have A Beautiful Valentine!
Left by valentine flower India on Jan 19, 2011 8:14 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article Just continue composing this kind of post.The time which was wasted in traveling for tuition now it can be utilized for studies.
Left by k03 turbo on Jan 21, 2011 9:01 AM

# Comment
Requesting Gravatar...
I hardly ever write comments on blogs, but your article urged me to praise your blog. Thanks for a great read, I'll be following you.
Left by Hcg and weight loss on Jan 24, 2011 11:43 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
I see a lot of people have some things to say about it in their remarks. I Would like to see more being wrote about it that's for sure.I will surely return often.
Left by File Management System on Feb 19, 2011 9:23 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Nice effort, very informative, this will help me to complete my task.
Left by send flowers germany on Feb 21, 2011 6:40 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Its true this code is currently just evaluating the current date and will always return true
Left by Gaz on Feb 22, 2011 11:47 AM

# re: jQuery Date Validation in Chrome Wholesale Evening Dresses
Requesting Gravatar...
We offer a selective range of exquisite dresses, with an extensive collection from A-line Wedding Dresses to Prom Dresses for you to choose from. We are devoted to provide you with fine contemporary and classic design dresses with reasonable price. Be inspired with our dresses products, you are the owner by spending much less than you expect. Fast shipping to UK, no sales tax.2010 new styles A-line Wedding Dresses,Beach Wedding Dresses,Evening Dresses,Prom Dresses on sale. Buy direct from the original online dresses retailer.http://www.wholesale-weddingdress.com/ wholesale wedding dresses

http://www.wholesale-promgowns.com/ Wholesale Prom Gowns
Left by Wholesale Evening Dresses on Mar 08, 2011 2:59 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have gone through it's details and got a lot of useful tips.
Left by Turkish Gulet Cruises on Mar 08, 2011 5:33 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
I Would like to see more being wrote about it that's for sure.I will surely return often.
Left by Discount cocktail Dresses on Mar 09, 2011 9:14 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Todd and I are equally Catholic, so inside the starting we made a decision to possess a entire mass to celebrate our union, in entrance of our family people and closest friends. I loved the church the instant I stepped foot in it practically 4 many years ago. My Mom walked me along the aisle while my eye have been viewing Todd, practically nothing else.http://www.simple-weddingdresses.com/
Left by Wholesale Bridal Dresses on Mar 11, 2011 2:01 AM

# Mr
Requesting Gravatar...
This is what I have been searching in many websites and I finally found it here. Amazing article. I am so impressed. Could never think of such a thing is possible with it...I think you have a great knowledge especially while dealings with such subjects.
Left by super affiliate coaching club on Mar 14, 2011 6:58 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Interesting topic what you have shared with us. Your writing skill is really very appreciative. I love when you share your views through the best articles.Keep sharing and posting articles like these.This article has helped me a lot.Keep posting this stuff.
Left by launch jacking on Mar 18, 2011 9:22 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
I am absolutely amazed at how terrific the stuff is on this site. I have saved this webpage and I truly intend on visiting the site in the upcoming days. Keep up the excellent work!

Left by Mothers day flowers Colombia on Apr 15, 2011 8:43 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
I wonder how you got so good. This is really a fascinating blog, lots of stuff that I can get into. One thing I just want to say is that your Blog is so perfect!
Left by Mothers day flowers Denmark on Apr 15, 2011 8:44 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
this is what I was looking for... thanks a ton!
Left by Rick on Apr 15, 2011 9:32 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
I Would like to see more being wrote about it that's for sure.I will surely return often.
Left by NBA Hats on May 03, 2011 9:14 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Nice post, thanks for sharing this wonderful and useful information with us
Left by Flowers UK on May 13, 2011 6:58 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
That is a pretty interesting post. Thanks for the info. such a very great post.
Left by Keychains on May 19, 2011 7:05 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
That is a pretty interesting post. Thanks for the info. such a very great post.
Left by airport parking heathrow on Jun 01, 2011 3:26 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
You raise many questions in my mind; you wrote a good post, but it is also thought provoking, and I will have to ponder it a bit more; I will return soon
Left by meet and greet on Jun 01, 2011 3:27 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
This is my first time I visit here. I found so much entertaining stuff in your blog. Keep up the excellent work.

Left by jessica parker perfume on Jun 03, 2011 6:17 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Thanx for the post ;-) London escorts Indian
Left by London Escorts on Jun 05, 2011 12:36 AM

# Nice
Requesting Gravatar...
It’s amazing, looking at the time and effort you put into your blog and detailed information you provide. I'll bookmark your blog and visit it weekly for your new posts.
Left by anam on Jun 25, 2011 6:00 PM

# Nice
Requesting Gravatar...
It’s amazing, looking at the time and effort you put into your blog and detailed information you provide. I'll bookmark your blog and visit it weekly for your new posts.


anam
Left by Four Seasons Condos for Sale on Jun 26, 2011 1:18 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Thank you!
Left by Christophe Lau on Jul 01, 2011 4:32 PM

# hello
Requesting Gravatar...
I have just recently found your blog and absolutely love the posts. Thank you, thank you. I am still .NET programming but can't wait to try these things in Ruby and it is a great help to see such clear examples. kids apron
Left by Kids aprons on Jul 19, 2011 8:49 AM

# hello
Requesting Gravatar...
Great things you’ve always shared with us. Thanks. Just continue composing this kind of post. The time which was wasted in traveling for tuition now it can be utilized for studies. Thanks for this knowledgeable blog. kids apron
Left by Kids aprons on Jul 19, 2011 8:50 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Excellent Post
Left by Costa Rica Real Estate on Jul 19, 2011 8:56 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
I agree. I love blogging like that. I found very interesting posts in this site. I hope there will be more...
pozycjonowanie stron wrocław
Left by greg on Aug 12, 2011 3:45 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Fully working solution

var d = new Date()
fecha=value.split("/");
d.setFullYear(fecha[2],fecha[1]-1,fecha[0]);

return this.optional(element) || !/Invalid|NaN/.test(d);
Left by nestor on Aug 31, 2011 6:12 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
In fact your creative writing skills have inspired me to get my own blog going now. Actually blogging is spreading its wings and growing quickly. Your write up is a great example.
Left by alquiler coches Canarias on Sep 15, 2011 9:11 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
This really is my current exceedingly amateur works, amazing dreams. Which i stubled out onto getting chilly tools by means of reminiscences tremendously in truth discussion. Coming from all including type of solutions from your subject material, it's enjoy this is a really in unbelievably well-liked web pages. Offer the unquestionably significant cost.
Left by Allison Glock Author on Oct 03, 2011 11:24 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
this will work fully good even in not English environment :
var d = new Date();
return this.optional(b)||!/Invalid|NaN/.test(new Date(d.toDateString(a))) ;
Left by Brahim EL AASSAL on Oct 10, 2011 8:01 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
This is a great article. Thanks for posting
Left by French Translation on Oct 12, 2011 6:40 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Very amazing and awesome post this one is! I really admire it and appreciate it! keep posting similar stuff!
Left by Lexus Parts on Oct 19, 2011 2:56 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
I should definitely say I'm impressed with your blog.
I had no trouble navigating through all the tabs as well as related info.
The site ended up being truly simple
Left by air condition drier on Oct 21, 2011 7:38 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
I enjoy some of content in the post.. please keep it up..
i want same like this from you... thanks .You have written nice post, I am gonna bookmark this page,
thanks for info. I actually appreciate your own position


Left by air conditioning compressor on Oct 22, 2011 8:46 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
I really like this article and you are given here really a wonderful information.I tried that & its really worked…
Left by venu on Oct 27, 2011 8:01 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
I really like this article and you are given here really a wonderful information.GOOD WORK
Left by venu on Oct 27, 2011 8:01 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Good – I should definitely say I'm impressed with your blog. I had no trouble navigating through all the tabs as well as related info. The site ended up being truly simple to access. Excellent job
Left by Vimax Reviews on Oct 31, 2011 6:21 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Very fine and nice and impressive and thought piece of info! he! I like!
Left by Mitsubishi Parts on Nov 03, 2011 1:37 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Thank you for sharing to us.there are many person searching about that now they will find enough resources by your post.I would like to join your blog anyway so please continue sharing with us
Left by Employee Referral Software on Nov 05, 2011 8:16 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Very informative post. I was looking for information about this topic and this post really helped me a lot. Thanks for sharing.

Left by cocteles on Nov 11, 2011 9:47 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Thanks a lot for sharing this info and with such a great outlook you have given to this page!
Left by Rolls Royce Parts on Nov 18, 2011 6:01 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Thanks for the first newsletter, I enjoyed reading it!Looking for more to come!Keep up on it!
Left by professional essay writers on Nov 25, 2011 7:55 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
This is a great article. Thanks for posting
Left by pharmaceutical sourcing event on Nov 26, 2011 12:11 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
I always avoid prophesying beforehand, because it is a much better policy to prophesy after the event has already taken place.
Left by paper writing service on Nov 27, 2011 11:42 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
I agree. I love blogging like that. I found very interesting posts in this site. I hope there will be more...
Left by web development services on Nov 28, 2011 10:55 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
I am following your site for a long time.We have visitors, write effective articles.Thank you all for your effort.
My English is not very good grammar errors may occur.
Left by Miami DUI Lawyer on Dec 07, 2011 12:23 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
I am very happy to find this blog.Thanks for creating the page! Im positive that it will be very popular. It has good and valuable content which is very rare these days.
Dental Assistant Training
Left by Medical on Dec 07, 2011 12:30 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
workout for me to go through your web page. It definitely stretches the limits with the mind when you go through very good info and make an effort to interpret it properlY.
Left by Hard Link Trades on Dec 14, 2011 6:36 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
I can see that you are putting a lots of efforts into your blog. Keep posting the good work.Some really helpful information in there. Bookmarked. Nice to see your site.
Fresh Global News
Left by The News Star on Dec 15, 2011 3:24 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Resources like the one you mentioned here will be very useful to me! I will post a link to this page on my blog. I am sure my visitors will find that very useful.
Small Kitchen Design
Left by Kitchen Improvements on Dec 15, 2011 3:31 AM

# sweet mano
Requesting Gravatar...
The world's biggest power is the youth and beauty of a woman.
Left by armaan on Dec 17, 2011 4:32 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Many thanks for making the sincere effort to explain this. I feel fairly strong about it and would like to read more. If it's OK, as you find out more in depth knowledge, would you mind writing more posts similar to this one with more information?
Left by mens clothing designer on Dec 22, 2011 12:42 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Very amazing and awesome post this one is! I really admire it and appreciate it! keep posting similar stuff!
Left by Maritime Geospatial Information on Jan 05, 2012 11:00 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
I must say that the jQuery date validation in chrome is a very complex thing and I must also say that you have done a great job by starting a discussion on this topic.
Left by Search name with phone number on Feb 14, 2012 6:15 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
This is an inspirational and fantastic writing for other writers and artists to follow this website provides such valuable information.
Left by music production on Mar 25, 2012 3:38 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
FINALLY =D, your post should be the first in Google, I had to search 3 houres until I found this. Your the best, thanks!
Left by Bert on May 25, 2012 1:12 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Thanks a lot for providing the great info that to using the nice info.
Left by undergraduate thesis on May 27, 2012 1:03 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
We had a fiddly issue with date validation in an ASP.NET MVC page failing for a valid date in Chrome, but passing in Firefox, IE etc. Tracing through our own code and xVal, the issue was narrowed down to the jQuery validation plugin (jquery.validate.js).
Left by calltracky on Jun 18, 2012 7:51 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
informations d'excellente qualité fournie, ce fut un plaisir d'apprendre tout ce genre de choses ici Du Proprio
Left by Du Proprio on Jun 19, 2012 8:00 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
how to use jquery validation for uk date
Left by David on Jul 21, 2012 8:16 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Fantastic article, great enjoyed reading through the material here, keep up the good work.
Left by Proxeemo on Aug 28, 2012 8:47 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
i have tested your code in chrome worked fine for me but the probleme , is it doesn't work in firefox web browser
Left by Rachid on Aug 31, 2012 12:04 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Um - did you actually test this workaround? toLocaleDateString() doesn't appear to accept arguments (tested in Chrome), so always returns *today's* date as a local string! Try the following in your Chrome console to see this: (new Date()).toLocaleDateString("13/12/2007")

I reported the bug in the Validate date method on the GitHub issue log and suggested a workaround until it's fixed:
https://github.com/jzaefferer/jquery-validation/issues/549

A quick copy of the workaround, for ease of reference:

jQuery().ready(function() {
// jQuery date validate fix: Chrome (at v22) validates as US date so fails UK (etc) dates like 13/1/2012
// - This is contrary to Validate docs which say order doesn't matter: http://docs.jquery.com/Plugins/Validation/Methods/date
// - To workaround, if validation fails, accept any string in "x/x/xx" format, where x = one or more digits
// - Credit: Neogic Web Solutions Ltd - www.neogic.com
jQuery.validator.methods.date = function(value, element) {
return this.optional(element) || !/Invalid|NaN/.test(new Date(value)) || /^(\d+)\/(\d+)\/(\d{2,})$/.test(value);
// OLD: return this.optional(element) || !/Invalid|NaN/.test(new Date(value));
}
}
Left by B Johnson on Nov 14, 2012 9:58 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Thanks for the code i tried it it works fine.
Market Research Reports Online
Left by Aaron on Mar 16, 2013 7:06 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
Thank you so much, this solved my problem.

In a quick note, you can extend jquery and apply this solution in a separate file so you would not have to change the jquery original source. Below is the solution:

// fix date validation for chrome
jQuery.extend(jQuery.validator.methods, {
date: function (value, element) {
// original version return this.optional(element) || !/Invalid|NaN/.test(new Date(value));
// correction for chrome bug of locale
var d = new Date();
return this.optional(element) || !/Invalid|NaN/.test(new Date(d.toLocaleDateString(value)));
}
});
Left by Eduardo Fernandes on Apr 17, 2013 1:26 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
None of the examples you give are parsing the date correctly. 29/10/2009 is not the 10th of May.

29 mod 12 == 5
5 == May
29/10/2009 as mm/dd/yyyy == (29 mod 12)/10/2009 == May 10th 2009 + the extra 24 months (29 - 5 = 24) == May 10th 2011

Chrome rejects the invalid date, the other browsers seem to compensate by adding the extra months.

Thanks,
Oli
Left by Oliver on Sep 02, 2013 2:49 PM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
am using date format like yy-mm-dd its working fine in mozilla but its showing exception in chrome am using for date validation jquery.validate.js


how to resolve it
Left by sravanthi on Mar 05, 2014 5:11 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
am using date format like yy-mm-dd its working fine in mozilla but its showing exception in chrome am using for date validation jquery.validate.js..


how to resolve it
Left by sravanthi on Mar 05, 2014 5:11 AM

# re: jQuery Date Validation in Chrome
Requesting Gravatar...
The best one
Left by Jayson on Apr 08, 2014 12:46 AM

Your comment:
 (will show your gravatar)
 


Copyright © Elton Stoneman | Powered by: GeeksWithBlogs.net | Join free