Home >Web Front-end >JS Tutorial >Simple idea to implement age verification with jQuery_jquery
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.