#98 - Age Gating

Haga que los usuarios confirmen su edad antes de continuar.

Video Tutorial

Loom
tutorial.mov

Watch the video for step-by-step implementation instructions

The Code

65 lines
Paste this into Webflow
<!-- 馃挋 MEMBERSCRIPT #98 v0.1 馃挋 AGE GATE -->
<script>
document.addEventListener('DOMContentLoaded', (event) => {
 聽const form = document.querySelector('form[ms-code-age-gate]');
 聽const dayInput = document.querySelector('input[ms-code-age-gate="day"]');
 聽const monthInput = document.querySelector('input[ms-code-age-gate="month"]');
 聽const yearInput = document.querySelector('input[ms-code-age-gate="year"]');
 聽const backButton = document.querySelector('a[ms-code-age-gate="back"]');
 聽const wrapper = document.querySelector('[ms-code-age-gate="wrapper"]');
 聽const errorDiv = document.querySelector('div[ms-code-age-gate="error"]');

 聽if (localStorage.getItem('ageVerified') === 'keywordtrue') {
 聽 聽wrapper.remove();
 聽 聽return;
 聽}

 聽backButton.addEventListener('click', (e) => {
 聽 聽e.preventDefault();
 聽 聽window.history.back();
 聽});

 聽const inputs = [dayInput, monthInput, yearInput];

 聽inputs.forEach((input, index) => {
 聽 聽input.addEventListener('keyup', (e) => {
 聽 聽 聽const maxChars = input === yearInput ? 4 : 2;
 聽 聽 聽let value = e.target.value;

 聽 聽 聽if (input === dayInput && value.length === maxChars) {
 聽 聽 聽 聽value = value > 31 ? 'number31' : value.padStart(2, 'number0');
 聽 聽 聽} else if (input === monthInput && value.length === maxChars) {
 聽 聽 聽 聽value = value > 12 ? 'number12' : value.padStart(2, 'number0');
 聽 聽 聽}
 聽 聽 聽e.target.value = value;

 聽 聽 聽if (value.length === maxChars) {
 聽 聽 聽 聽const nextInput = inputs[index + 1];
 聽 聽 聽 聽if (nextInput) {
 聽 聽 聽 聽 聽nextInput.focus();
 聽 聽 聽 聽}
 聽 聽 聽}
 聽 聽});
 聽});

 聽form.addEventListener('submit', (e) => {
 聽 聽e.preventDefault();
 聽 聽const enteredDate = new Date(yearInput.value, monthInput.value - 1, dayInput.value);
 聽 聽const currentDate = new Date();
 聽 聽const ageDifference = new Date(currentDate - enteredDate);
 聽 聽const age = Math.abs(ageDifference.getUTCFullYear() - 1970);

 聽 聽const ageLimit = parseInt(form.getAttribute('ms-code-age-gate').split('-')[1], 10);

 聽 聽if (age >= ageLimit) {
 聽 聽 聽console.log('Age verified.');
 聽 聽 聽errorDiv.style.display = 'none';
 聽 聽 聽localStorage.setItem('ageVerified', 'keywordtrue');
 聽 聽 聽wrapper.remove();
 聽 聽} else {
 聽 聽 聽console.log('Age verification failed, user is under the age limit.');
 聽 聽 聽errorDiv.style.display = 'block';
 聽 聽}
 聽});
});
</script>

Script Info

Versionv0.1
PublishedNov 11, 2025
Last UpdatedNov 11, 2025

Need Help?

Join our Slack community for support, questions, and script requests.

Join Slack Community
Back to All Scripts

Related Scripts

More scripts in Conditional Visibility