18 - Reject gmail and other non-business emails from Webflow form submission

Hey I used you hack18 about form submissions, but for some reason It’s not working.
I recorded a video about this - Loom | Free Screen & Video Recording Software | Loom

Sharable file - https://preview.webflow.com/preview/adaptiveco-conor-e018f5f5-24be658231b61?utm_medium=preview_link&utm_source=designer&utm_content=adaptiveco-conor-e018f5f5-24be658231b61&preview=06fffd8eb331d5ed9e1c542c6a98545a&pageId=65c613de5491bc6185e49af4&workflow=preview

@Support-Luis Any Ideas?

Hey @sepels41! This is an old hack, let me see what I can do.

hey @sepels41! The issue might have come from having multiple forms on the same page. I’ve modified the script into vanilla JS and this should handle both forms on the page.

<script>
  document.addEventListener('DOMContentLoaded', function () {
    // Make an array of invalid domains
    const invalidDomains = ['gmail.com', 'yahoo.com', 'hotmail.com', 'competitor.com'];

    // Get all submit buttons and email fields
    const submitBtns = document.querySelectorAll('.hack-button');
    const emailFields = document.querySelectorAll('.hack18-email');

    // Iterate over each submit button
    submitBtns.forEach((submitBtn, index) => {
      // Add click event listener to each submit button
      submitBtn.addEventListener('click', function (event) {
        // Get the email field corresponding to the clicked button
        const email = emailFields[index];

        // Split email at '@' character to get domain
        const domainPart = email.value.split('@')[1];

        // Check if email is valid
        const isValidEmail = invalidDomains.indexOf(domainPart) === -1;

        // Log whether email is valid or not (for testing purposes)
        if (!isValidEmail) {
          // Clear email field
          email.value = '';
          // Add a 'use business mail' placeholder
          email.setAttribute('placeholder', 'Please enter a business email');
          // Prevent form submission
          event.preventDefault();
        }
      });
    });
  });
</script>

I’m not sure what might be happening with the contact form on the modal. This script shouldn’t affect any behavior other than preventing submission if the email is not valid.