Revision: 61597
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at December 24, 2012 18:44 by agarcim
Initial Code
//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 || "/"); if (SplitValue.length != 3) { return false; } //Auto detection of indexes if (dayIdx === undefined || monthIdx === undefined || yearIdx === undefined) { if (SplitValue[0] > 31) { yearIdx = 0; monthIdx = 1; dayIdx = 2; } else { 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 OK = true; if (!(SplitValue[DayIndex].length == 1 || SplitValue[DayIndex].length == 2)) { OK = false; } if (OK && !(SplitValue[MonthIndex].length == 1 || SplitValue[MonthIndex].length == 2)) { OK = false; } if (OK && SplitValue[YearIndex].length != 4) { OK = false; } if (OK) { 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]; if (OK = (Month <= 12 && Month > 0)) { 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; } catch (e) { return false; } }
Initial URL
http://www.codeproject.com/Tips/144113/JavaScript-Date-Validation
Initial Description
Returns true if the string 'value' is a valid date.
Initial Title
Validate if a string is a Date
Initial Tags
javascript, validation
Initial Language
Groovy