Last active
August 7, 2019 06:49
-
-
Save ABooooo/ca9ca0361316eacf622c7a83bd9d40df to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| function isValidDate(dateString) | |
| { | |
| // First check for the pattern | |
| var regex_date = /^\d{4}\-\d{1,2}\-\d{1,2}$/; | |
| if(!regex_date.test(dateString)) | |
| { | |
| return false; | |
| } | |
| // Parse the date parts to integers | |
| var parts = dateString.split("-"); | |
| var day = parseInt(parts[2], 10); | |
| var month = parseInt(parts[1], 10); | |
| var year = parseInt(parts[0], 10); | |
| // Check the ranges of month and year | |
| if(year < 1000 || year > 3000 || month == 0 || month > 12) | |
| { | |
| return false; | |
| } | |
| var monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]; | |
| // Adjust for leap years | |
| if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) | |
| { | |
| monthLength[1] = 29; | |
| } | |
| // Check the range of the day | |
| return day > 0 && day <= monthLength[month - 1]; | |
| } | |
| https://stackoverflow.com/questions/6177975/how-to-validate-date-with-format-mm-dd-yyyy-in-javascript |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment