Code

Age Gate Modal Overlay

Add a birthday/age gate modal confirmation to your Webflow site via this simple Javascript code snippet.

Age Gate Modal Overlay preview

Add a birthday/age gate modal confirmation to your Webflow site via this simple Javascript code snippet.

How to add it

  1. Add the Javascript code snippet to your Webflow custom code before the closing body tag
  2. Update the classes and IDs to your own IDs and classes

The code

Javascript
                      <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>

                    

Browse similar code snippets

Categories

Language

More from Corey Moen

Profile →

Related Webflow questions

All answers →

Similar code snippets

All code snippets →