1

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!

Emy O. W
  • 31
  • 3
  • you can start by reading here http://stackoverflow.com/questions/1353684/detecting-an-invalid-date-date-instance-in-javascript – roullie May 29 '15 at 10:25
  • why don't you are using datepicker? – Bhavin Panchani May 29 '15 at 10:35
  • I can't use a datepicker since I'm building this for a client following their design and they wan't to have three separate fields for day/month/year, without datepicker. Also I'm pretty new to javascript. I've looked around a lot, but haven't found anything that I could understand. – Emy O. W May 29 '15 at 10:47
  • Pls define _invalid date_ in terms of your requirements for us to be a ble to help. – lshettyl May 29 '15 at 11:12
  • Okay, just added it to the question. _invalidDate_ could be if they enter letter instead or numbers, but preferably it would be defined if day isn't a number between 1-31, if month isn't a number between 1-12 and if year doesn't start with 19XX or 20XX. – Emy O. W May 29 '15 at 11:45

1 Answers1

1

I've always found the best way to have a datepicker with certain rules (e.g over 18), is to display it in Year, Month, Day format.

I created this for a small project I was working on, maybe it will help you.

jsfiddle.net/adamjld/3gdxkL42

adamjld
  • 310
  • 1
  • 9