/ Published in: Groovy
Returns true if the string 'value' is a valid date.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
//Value parameter - required. All other parameters are optional. function isDate(value, sepVal, dayIdx, monthIdx, yearIdx) { try { value = value.replace(/-/g, "/").replace(/\./g, "/"); var SplitValue = value.split(sepVal || "/"); } //Auto detection of indexes yearIdx = 0; monthIdx = 1; dayIdx = 2; yearIdx = 2; monthIdx = 1; dayIdx = 0; } } //Change the below values to determine which format of date you wish to check. It is set to dd/mm/yyyy by default. var DayIndex = dayIdx !== undefined ? dayIdx : 0; var MonthIndex = monthIdx !== undefined ? monthIdx : 1; var YearIndex = yearIdx !== undefined ? yearIdx : 2; } } } var Day = parseInt(SplitValue[DayIndex], 10); var Month = parseInt(SplitValue[MonthIndex], 10); var Year = parseInt(SplitValue[YearIndex], 10); var MonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; var LeapYear = (((Year % 4) == 0) && ((Year % 100) != 0) || ((Year % 400) == 0)); MonthDays[1] = (LeapYear ? 29 : 28); OK = Day > 0 && Day <= MonthDays[Month - 1]; } } return OK; } } }
URL: http://www.codeproject.com/Tips/144113/JavaScript-Date-Validation