Home >Web Front-end >JS Tutorial >Simple idea to implement age verification with jQuery_jquery

Simple idea to implement age verification with jQuery_jquery

WBOY
WBOYOriginal
2016-05-16 15:13:551290browse

This code assumes that the environment is a "form" with the ID age-form, and the three IDs are "day", "month", and "year".

$("#age-form").submit(function(){
  var day = $("#day").val();
  var month = $("#month").val();
  var year = $("#year").val();
  var age = 18;
  var mydate = new Date();
  mydate.setFullYear(year, month-1, day);

  var currdate = new Date();
  currdate.setFullYear(currdate.getFullYear() - age);
  if ((currdate - mydate) < 0){
    alert("Sorry, only persons over the age of " + age + " may enter this site");
    return false;
  }
  return true;
});

Maybe you want to use a more elegant prompt method than alert. And it should be verified again on the server side, otherwise it can only be verified on the client side with js enabled.

Anyway, the meaning of the code is to let the user fill in the year and month of birth, and then calculate whether it is younger than the age required by the website based on the current time, and prompt if it is less than the age required by the website.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn