Skip to content

Instantly share code, notes, and snippets.

@nocodesupplyco
Created December 19, 2022 16:51
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save nocodesupplyco/d617f4a788e9548f2f6f9849d4d15fad to your computer and use it in GitHub Desktop.
Birthday Age Gate Modal Overlay
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-cookie/2.2.1/js.cookie.js"></script>
<script>
//Modified from Finsweets age gate redirect hack: https://www.finsweet.com/hacks/21/
// if no validAge cookie then show the age gate
if (!Cookies.get("validAge")) {
$(".age-gate-modal-wrapper").show();
}
// when form is submitted check if the birthday is above 18 years old
$("#verify-form").on("submit", function (e) {
// get year, month & day values
const year = $("#verify-year").val();
const month = $("#verify-month").val();
const day = $("#verify-day").val();
// make date string
const date = year + "/" + month + "/" + day;
// if user's age >= 18
if (getAge(date) >= 18) {
// prevent form from submitting
e.preventDefault();
// give the user a validAge cookie
Cookies.set("validAge", true, {
expires: 365,
});
// hide the age gate popup
$(".age-gate-modal-wrapper").hide();
}
});
// getAge function
function getAge(dateString) {
const today = new Date();
const birthDate = new Date(dateString);
let age = today.getFullYear() - birthDate.getFullYear();
const month = today.getMonth() - birthDate.getMonth();
if (month < 0 || (month === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment