Valid Date routine
Valid Date routine
I have a current set of javascript that validates to see if a date selected is valid. My dates are picked from 3 different select boxes; one for month, day, and year. I'm running into an issue where if someone picks 2/31/2014, it sees it as 3/2/2014 when I really want it to just return invalid. Presumably this is happening because February only has 28 days so it's just adding a few more onto it. I was hoping someone could help me update my code to make it work correctly! Any help would be much appreciated.
- function validateDob(condition) {
- var vYear = DES_GetById("<%= ddlDateOfBirthYear.ClientID%>").value;
- var vMonth = DES_GetById("<%= ddlDateOfBirthMonth.ClientID%>").value;
- var vDay = DES_GetById("<%= ddlDateOfBirthDay.ClientID%>").value;
- var ddlYear = DES_GetById("<%= ddlDateOfBirthYear.ClientID%>")
- var ddlMonth = DES_GetById("<%= ddlDateOfBirthMonth.ClientID%>")
- var ddlDay = DES_GetById("<%= ddlDateOfBirthDay.ClientID%>")
- if (vYear != 0 && vMonth != 0 && vDay != 0) {
- //var vFull = vMonth + '/' + vDay + '/' + vYear
- if (isDate(vYear, vMonth, vDay)) {
- $(ddlYear).removeClass("input-error");
- $(ddlMonth).removeClass("input-error");
- $(ddlDay).removeClass("input-error");
- return 1;
- }
- }
- $(ddlYear).addClass("input-error");
- $(ddlMonth).addClass("input-error");
- $(ddlDay).addClass("input-error");
- return -1;
- }
- function getYear(d) {
- return (d < 1000) ? d + 1900 : d;
- }
- function isDate(year, month, day) {
- // month argument must be in the range 1 - 12
- month = month - 1; // javascript month range : 0- 11
- var tempDate = new Date(year, month, day);
- if ((getYear(tempDate.getYear()) == year) &&
- (month == tempDate.getMonth()) &&
- (day == tempDate.getDate()))
- return true;
- else
- return false
- }