I'm setting up an age gate for a clients page that contains alcohol. The visitor must fill in day, month and birthyear and also country. The form is connected to minimum purchase ages in different countries.
I've managed to get the form to redirect to startpage if they are old enough or display "Sorry, you're not old enough to enter this site" if they are too young. So far so good.
But I also need the form to display "Please enter a valid date of birth" if they put in something that isn't an actual date.
Is there a way to define var invalidDate =
invalidDate could be if they enter letters instead of numbers, but also much appreciated would be if invalid for Day is anything that isn't between number 1-31, for month anything that isn't number 1-12 and for year anything that doesn't start with 19XX or 20XX.
Sorry if I'm being unclear...
The form as it is right now (most countries removed because the list was so long):
<script type="text/javascript">
var
$ = $||jQuery;
countries = {
"Sweden": 20,
"Denmark": 18,
"Other": 21,
}
$(document).ready(function() {
$("form").submit(function(e) {
e.preventDefault();
var day = parseInt($("input[name=day-796]").val());
var month = parseInt($("input[name=month-664]").val() - 1);
var year = parseInt($("input[name=year-262]").val());
var dateOfBirth = new Date(year, month, day, 0, 0, 0, 0);
var ageDifMs = Date.now() - dateOfBirth.getTime();
var ageDate = new Date(ageDifMs);
var age = Math.abs(ageDate.getUTCFullYear() - 1970);
var country = $("select").val();
var hasAccess = age >= countries[country];
var noAccess = age <= countries[country];
if (hasAccess) {
window.location.href = "www.startpageURL.com";
} else if (noAccess) {
document.getElementById("warning_output").innerHTML = "Sorry, you're not old enough to enter this site.";
}
})
})
</script>
Any suggestions would be much appreciated!