The Little Guy Posted June 3, 2012 Share Posted June 3, 2012 Is there any why to test if a given date (YYYY-MM-DD) is a valid date? I would like to check via javascript before I check via php. Anyone know of a good way to test? Quote Link to comment https://forums.phpfreaks.com/topic/263562-date-validation/ Share on other sites More sharing options...
trq Posted June 3, 2012 Share Posted June 3, 2012 You can test for a valid date format. JavaScript, like a lot of other languages has a regex engine. Quote Link to comment https://forums.phpfreaks.com/topic/263562-date-validation/#findComment-1350785 Share on other sites More sharing options...
The Little Guy Posted June 3, 2012 Author Share Posted June 3, 2012 Here is what I came up with, but I don't know if it is good or not: function validDate(y, m, d){ y = parseInt(y); m = parseInt(m); d = parseInt(d); ld = new Date(y, m, 0); lastDate = ld.getDate(); date = new Date(y, m, d); if(y != date.getFullYear()) return false; if(m != date.getMonth()) return false; if(d != date.getDate() || date.getDate() > lastDate) return false; return true; } Quote Link to comment https://forums.phpfreaks.com/topic/263562-date-validation/#findComment-1350848 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.