Last active
May 5, 2019 09:05
-
-
Save sosroInSpace/cf699597a89681140a508ae2a5ec05f0 to your computer and use it in GitHub Desktop.
Validate users birthdate to be at least 18 years old
This file contains 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
/*-------------------------------------------------------------------------- | |
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