title: jQuery - Help to define invalid date in age gate
body: <p>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.</p>
  
  <p>I've managed to get the form to redirect to startpage if they are old enough or display "<em>Sorry, you're not old enough to enter this site</em>" if they are too young. So far so good.</p>
  
  <p>But I also need the form to display "<em>Please enter a valid date of birth</em>" if they put in something that isn't an actual date.</p>
  
  <p>Is there a way to define <strong>var invalidDate =</strong> </p>
  
  <p>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. </p>
  
  <p>Sorry if I'm being unclear...</p>
  
  <p>The form as it is right now (most countries removed because the list was so long):</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
  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 &gt;= countries[country];
          var noAccess = age &lt;= 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.";
          }
      })
  })
  &lt;/script&gt;
  </code></pre>
  
  <p>Any suggestions would be much appreciated!</p>