Skip to content

Instantly share code, notes, and snippets.

@sosroInSpace
Last active May 5, 2019 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sosroInSpace/cf699597a89681140a508ae2a5ec05f0 to your computer and use it in GitHub Desktop.
Save sosroInSpace/cf699597a89681140a508ae2a5ec05f0 to your computer and use it in GitHub Desktop.
Validate users birthdate to be at least 18 years old
/*--------------------------------------------------------------------------
VALIDATE BIRTH DATE TO BE ATLEAST 18 YEARS OLD
--------------------------------------------------------------------------*/
function validateBirthDate() {
// date formmat day/month/year = 23/02/2001
var dateValue = $('#customer_birth_date').val();
var dateInput = $('#customer_birth_date');
var match = /(\d+)\/(\d+)\/(\d+)/.exec(dateValue);
if (match) {
var day = Number(match[1]);
var month = Number(match[2]);
var year = Number(match[3]);
// Check if over 18.
var date = new Date();
date.setFullYear(year, month - 1, day);
var eighteenYears = new Date();
eighteenYears.setFullYear(eighteenYears.getFullYear() - 18);
if (date > eighteenYears) {
console.log("under 18 years old");
// under 18.
}
else {
console.log('18 years old');
// 18 or older
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment