Created
February 7, 2017 12:55
-
-
Save fernandosavio/05fe9f82811375d0d5a00124e91c5dfb to your computer and use it in GitHub Desktop.
Validate a date (DD/MM/YYYY).
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 valid_date(str_data) { | |
| var regex = /^\d{2}\/\d{2}\/\d{4}$/, | |
| d, m, y; | |
| str_data = str_data.trim(); | |
| if (!regex.test(str_data)) { | |
| return false | |
| } | |
| str_data = str_data.split('/') | |
| d = +str_data[0]; | |
| m = +str_data[1] - 1; | |
| y = +str_data[2]; | |
| nova_data = new Date(y, m, d); | |
| // if the numbers match it means JS parsed a valid date | |
| return ( | |
| nova_data.getDate() == d && | |
| nova_data.getMonth() == m && | |
| nova_data.getFullYear() == y | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment