Last active
December 19, 2015 10:59
-
-
Save imjakechapman/5944507 to your computer and use it in GitHub Desktop.
function to check through a pre formatted birthday (ex: '08/09/1988') and calculates if your over 18.
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
| // vars | |
| var birthday = '08/09/1988'; | |
| var d = new Date(); | |
| var month = d.getMonth()+1; | |
| var day = d.getDate(); | |
| // double digit month | |
| ((''+month).length<2 ? '0' : '') + month | |
| // double digit day | |
| ((''+day).length<2 ? '0' : '') + day | |
| // run through the year, month and then day to check if over 18 years old. | |
| var birthdayCheck = function(birthday) { | |
| var bday = birthday.split('/'); | |
| if (bday[2] < 1995) { | |
| // person is older then 18 already, allow submission | |
| return true; | |
| } else if (bday[2] > 1995) { | |
| // not 18 till at next year, return false | |
| return false; | |
| } else if (bday[2] = 1995) { | |
| // birthday is this year, check the month | |
| if(bday[0] < ((''+month).length<2 ? '0' : '') + month ) { | |
| // birthday has already happened this year, allow submission | |
| return true; | |
| } else if (bday[0] > ((''+month).length<2 ? '0' : '') + month ) { | |
| // birthday hasn't happened yet, return false; | |
| return false; | |
| } else if (bday[0] = ((''+month).length<2 ? '0' : '') + month ) { | |
| // birthday is in same month, check the day. | |
| if (bday[1] < ((''+day).length<2 ? '0' : '') + day ) { | |
| // birthday has already happened this month, happy late birthday | |
| return true; | |
| } else if (bday[1] > ((''+day).length<2 ? '0' : '') + day ) { | |
| // birthday is coming up, but not quite there yet. | |
| return false; | |
| } else if (bday[1] = ((''+day).length<2 ? '0' : '') + day ) { | |
| // it's your birthday! HAPPY BIRTHDAY, allow submission | |
| alert('happy birthday'); | |
| return true; | |
| } | |
| } else { | |
| return false; | |
| } | |
| } else { | |
| return false; | |
| } | |
| }; | |
| // check to see if birthday validates | |
| if(birthdayCheck(birthday)) { | |
| // birthday validated do something | |
| } else { | |
| alert('Naughty Naugty, you are not 18 as of today'); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment