Skip to content

Instantly share code, notes, and snippets.

@imjakechapman
Last active December 19, 2015 10:59
Show Gist options
  • Select an option

  • Save imjakechapman/5944507 to your computer and use it in GitHub Desktop.

Select an option

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.
// 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