Created
October 18, 2016 21:57
-
-
Save saintach/fd6218f377bb6fc79ad7005e28ba3e55 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//e.g. input 2016-10-18 | |
function validateDateInputHelper(val) { | |
if (!val) return true; | |
var parts = val.split('-'); | |
if (parseInt(parts[0], 10) == 0 || parts[1] == '00' || parts[2] == '00') { | |
return false; | |
} | |
var year = parseInt(parts[0], 10); | |
var month = parseInt(parts[1], 10); | |
var day = parseInt(parts[2], 10); | |
if (year && year.length == 4 && year < 1000 || year > 3000 ) { | |
return false; | |
} | |
if (month && month.length == 2 && month > 12) { | |
return false; | |
} | |
if (day) { | |
var monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]; | |
// Consider leap years | |
if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) | |
{ | |
monthLength[1] = 29; | |
} | |
return day <= monthLength[month - 1]; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment