Elton Stoneman

  Home  |   Contact  |   Syndication    |   Login
  120 Posts | 0 Stories | 3607 Comments | 0 Trackbacks

News

Archives

Post Categories

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

    },

 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted on Thursday, October 29, 2009 2:20 PM

Feedback

# re: jQuery Date Validation in Chrome 3/5/2010 6:26 PM Mark@School grants
Thanks for the simple yet useful tutorial!

# re: jQuery Date Validation in Chrome 3/27/2010 10:20 AM dedicated server-47
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.

# re: jQuery Date Validation in Chrome 4/6/2010 6:52 AM Bulk SMS
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.

# re: jQuery Date Validation in Chrome 4/15/2010 1:02 PM steven
thank u very much!

# re: jQuery Date Validation in Chrome 4/24/2010 1:12 PM More On Me
Thank you very very much.

# re: jQuery Date Validation in Chrome 4/28/2010 2:42 AM nutritionals_74
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.

# re: jQuery Date Validation in Chrome 5/10/2010 2:51 PM Planos de Hospedagem
Exactly what i was looking for.
Thanks for this informative entry.

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

# re: jQuery Date Validation in Chrome 6/11/2010 7:41 AM platinum protection85
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.

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

# re: jQuery Date Validation in Chrome 7/9/2010 6:22 AM web host44
Nice post having excellent contents.I agree with most of what you are saying here.Thank you so much for sharing.Keep blogging.

# re: jQuery Date Validation in Chrome 7/13/2010 8:13 AM fadi
very nice tuto
thank you

# re: jQuery Date Validation in Chrome 8/6/2010 6:52 AM Ecommerce web design
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…


# re: jQuery Date Validation in Chrome 8/25/2010 10:06 AM Sewage Pumps
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.

# re: jQuery Date Validation in Chrome 8/31/2010 11:36 PM Sean Tomlins
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.

# re: jQuery Date Validation in Chrome 9/1/2010 12:45 AM Sean Tomlins
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

# re: jQuery Date Validation in Chrome 9/21/2010 12:18 PM Mad
Seems to be good only with chrome, not IE, non FF...

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

# re: jQuery Date Validation in Chrome 9/25/2010 5:29 AM LEED Certification
Your website is really useful for many people I think. You helped me in various situations because I many times I have found the useful information which was really important for me in your site.

# re: jQuery Date Validation in Chrome 10/1/2010 12:32 PM email marketing services
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

# re: jQuery Date Validation in Chrome 10/21/2010 1:01 PM directory submission
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.

# re: jQuery Date Validation in Chrome 11/19/2010 7:03 AM airport parking
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.

# re: jQuery Date Validation in Chrome 11/23/2010 5:17 PM business to business directories
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.

# re: jQuery Date Validation in Chrome 11/29/2010 9:15 PM heating systems monroe mi
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

# re: jQuery Date Validation in Chrome 11/29/2010 9:55 PM accent reduction training
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!

# re: jQuery Date Validation in Chrome 12/8/2010 10:37 AM this doesn't work
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.

#  Craft show listings 1/2/2011 5:21 PM Craftshow
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



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

# re: jQuery Date Validation in Chrome 1/21/2011 9:01 AM k03 turbo
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.

# Comment 1/24/2011 11:43 AM Hcg and weight loss
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.

# re: jQuery Date Validation in Chrome 2/19/2011 9:23 AM File Management System
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.


# re: jQuery Date Validation in Chrome 2/21/2011 6:40 AM send flowers germany
Nice effort, very informative, this will help me to complete my task.

# re: jQuery Date Validation in Chrome 2/22/2011 11:47 AM Gaz
Its true this code is currently just evaluating the current date and will always return true

# re: jQuery Date Validation in Chrome Wholesale Evening Dresses 3/8/2011 2:59 AM Wholesale Evening Dresses
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


# re: jQuery Date Validation in Chrome 3/8/2011 5:33 AM Turkish Gulet Cruises
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.

# re: jQuery Date Validation in Chrome 3/9/2011 9:14 AM Discount cocktail Dresses
I Would like to see more being wrote about it that's for sure.I will surely return often.


# re: jQuery Date Validation in Chrome 3/11/2011 2:01 AM Wholesale Bridal Dresses
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/

# Mr 3/14/2011 6:58 AM super affiliate coaching club
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.


# re: jQuery Date Validation in Chrome 3/18/2011 9:22 AM launch jacking
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.


# re: jQuery Date Validation in Chrome 4/15/2011 8:43 AM Mothers day flowers Colombia
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!



# re: jQuery Date Validation in Chrome 4/15/2011 8:44 AM Mothers day flowers Denmark
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!

# re: jQuery Date Validation in Chrome 4/15/2011 9:32 AM Rick
this is what I was looking for... thanks a ton!

# re: jQuery Date Validation in Chrome 5/3/2011 9:14 AM NBA Hats
I Would like to see more being wrote about it that's for sure.I will surely return often.

# re: jQuery Date Validation in Chrome 5/13/2011 6:58 AM Flowers UK
Nice post, thanks for sharing this wonderful and useful information with us

# re: jQuery Date Validation in Chrome 5/19/2011 7:05 PM Keychains
That is a pretty interesting post. Thanks for the info. such a very great post.


# re: jQuery Date Validation in Chrome 6/1/2011 3:26 PM airport parking heathrow
That is a pretty interesting post. Thanks for the info. such a very great post.

# re: jQuery Date Validation in Chrome 6/1/2011 3:27 PM meet and greet
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

# re: jQuery Date Validation in Chrome 6/3/2011 6:17 PM jessica parker perfume
This is my first time I visit here. I found so much entertaining stuff in your blog. Keep up the excellent work.



# re: jQuery Date Validation in Chrome 6/5/2011 12:36 AM London Escorts
Thanx for the post ;-) London escorts Indian

# Nice 6/25/2011 6:00 PM anam
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.


# Nice 6/26/2011 1:18 PM Four Seasons Condos for Sale
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

# re: jQuery Date Validation in Chrome 7/1/2011 4:32 PM Christophe Lau
Thank you!

# hello 7/19/2011 8:49 AM Kids aprons
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

# hello 7/19/2011 8:50 AM Kids aprons
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

# re: jQuery Date Validation in Chrome 7/19/2011 8:56 PM Costa Rica Real Estate
Excellent Post

# re: jQuery Date Validation in Chrome 8/12/2011 3:45 PM greg
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

# re: jQuery Date Validation in Chrome 8/31/2011 6:12 PM nestor
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);

# re: jQuery Date Validation in Chrome 9/15/2011 9:11 PM alquiler coches Canarias
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.

# re: jQuery Date Validation in Chrome 10/3/2011 11:24 PM Allison Glock Author
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.

# re: jQuery Date Validation in Chrome 10/10/2011 8:01 AM Brahim EL AASSAL
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))) ;

# re: jQuery Date Validation in Chrome 10/12/2011 6:40 PM French Translation
This is a great article. Thanks for posting

# re: jQuery Date Validation in Chrome 10/19/2011 2:56 PM Lexus Parts
Very amazing and awesome post this one is! I really admire it and appreciate it! keep posting similar stuff!

# re: jQuery Date Validation in Chrome 10/21/2011 7:38 PM air condition drier
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

# re: jQuery Date Validation in Chrome 10/22/2011 8:46 PM air conditioning compressor
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




# re: jQuery Date Validation in Chrome 10/27/2011 8:01 AM venu
I really like this article and you are given here really a wonderful information.I tried that & its really worked…

# re: jQuery Date Validation in Chrome 10/27/2011 8:01 AM venu
I really like this article and you are given here really a wonderful information.GOOD WORK

# re: jQuery Date Validation in Chrome 10/31/2011 6:21 PM Vimax Reviews
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

# re: jQuery Date Validation in Chrome 11/3/2011 1:37 PM Mitsubishi Parts
Very fine and nice and impressive and thought piece of info! he! I like!

# re: jQuery Date Validation in Chrome 11/5/2011 8:16 AM Employee Referral Software
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

# re: jQuery Date Validation in Chrome 11/11/2011 9:47 PM cocteles
Very informative post. I was looking for information about this topic and this post really helped me a lot. Thanks for sharing.



# re: jQuery Date Validation in Chrome 11/18/2011 6:01 PM Rolls Royce Parts
Thanks a lot for sharing this info and with such a great outlook you have given to this page!

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


# re: jQuery Date Validation in Chrome 11/26/2011 12:11 PM pharmaceutical sourcing event
This is a great article. Thanks for posting

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


# re: jQuery Date Validation in Chrome 11/28/2011 10:55 AM web development services
I agree. I love blogging like that. I found very interesting posts in this site. I hope there will be more...

# re: jQuery Date Validation in Chrome 12/7/2011 12:23 PM Miami DUI Lawyer
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.

# re: jQuery Date Validation in Chrome 12/7/2011 12:30 PM Medical
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

# re: jQuery Date Validation in Chrome 12/14/2011 6:36 AM Hard Link Trades
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.

# re: jQuery Date Validation in Chrome 12/15/2011 3:24 AM The News Star
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

# re: jQuery Date Validation in Chrome 12/15/2011 3:31 AM Kitchen Improvements
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

# sweet mano 12/17/2011 4:32 PM armaan
The world's biggest power is the youth and beauty of a woman.


# re: jQuery Date Validation in Chrome 12/22/2011 12:42 PM mens clothing designer
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?

# re: jQuery Date Validation in Chrome 1/5/2012 11:00 AM Maritime Geospatial Information
Very amazing and awesome post this one is! I really admire it and appreciate it! keep posting similar stuff!

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: