document.addEventListener("DOMContentLoaded", function () { // 1. Select all MetForm elements on the page const forms = document.querySelectorAll(".metform-form-content"); forms.forEach(function (form) { form.addEventListener("submit", function (event) { // 2. Find the email input field inside this specific form const emailInput = form.querySelector('input[type="email"]'); if (!emailInput) return; const emailValue = emailInput.value.trim().toLowerCase(); // 3. Define the list of forbidden free email domains const blockedDomains = ["gmail.com", "hotmail.com", "outlook.com", "live.com", "yahoo.com"]; // 4. Extract the domain from the entered email address const emailParts = emailValue.split("@"); if (emailParts.length !== 2) return; const emailDomain = emailParts[1]; // RESTORED: Targets the domain string after the @ // 5. Check if the domain is in our blocked list if (blockedDomains.includes(emailDomain)) { // Prevent the form from submitting to MetForm's servers event.preventDefault(); event.stopPropagation(); // 6. Handle error messaging removeExistingError(form); showErrorMessage(emailInput, "Please use a business email address. Free email providers (Gmail, Hotmail, etc.) are not allowed."); } else { removeExistingError(form); } }, true); // Use capturing phase to intercept MetForm's submit handler early }); // Helper function to inject a clean error message below the input field function showErrorMessage(inputElement, message) { inputElement.style.borderColor = "#ff4d4d"; const errorDiv = document.createElement("div"); errorDiv.className = "metform-custom-error"; errorDiv.innerText = message; errorDiv.style.color = "#ff4d4d"; errorDiv.style.fontSize = "13px"; errorDiv.style.marginTop = "5px"; errorDiv.style.fontWeight = "500"; inputElement.parentNode.appendChild(errorDiv); } // Helper function to clear previous error messages on retry function removeExistingError(formElement) { const existingError = formElement.querySelector(".metform-custom-error"); if (existingError) { existingError.remove(); } const emailInput = formElement.querySelector('input[type="email"]'); if (emailInput) { emailInput.style.borderColor = ""; } } });