Form validation is essential when you want to avoid spam or ensure that only certain types of email addresses are used in your website’s forms.
Webflow’s basic form functionality might not cover all your needs, especially if you want to restrict personal email providers (like Gmail or Yahoo) to maintain higher-quality leads. By adding custom validation, you can block unwanted email addresses in real-time — no extra tools needed.
In order to block personal email addresses, we’ll add a small JavaScript code snippet that runs right in the user’s browser. The code does the following:
First, you need to assign specific IDs to your form elements so the validation script can find and control them seamlessly. Follow these instructions exactly:
Open your form in the Webflow Designer
Select your email input field:
Select your form’s submit button:
Add a text element for error messages below the submit button:
For all of these instructions, please make sure to use the exact IDs mentioned. Do not use any other ID, as otherwise the script won't be able to locate this form input.
We need to create two CSS classes that give users immediate visual feedback about the form’s status. By applying these classes dynamically as the user interacts with the form, we guide them towards making the necessary corrections before attempting to re-submit.
.deactivated class (for the submit button)
You might also consider adjusting other styles (like changing the button color) to properly match your branding and website design style.
.show class (for the error message)
By combining these classes, you create an immediate and intuitive feedback loop. When a user enters an invalid email address, the submit button becomes visibly unavailable, and a helpful error message appears. This guides them to correct their input before trying again, resulting in a smoother, more user-friendly experience.
Now that we’ve prepared our form elements and styling, the next step is to integrate the custom validation logic into your Webflow project. We’ll add a small JavaScript snippet that monitors input fields, applies our validation rules, and provides immediate feedback to users.
For forms on specific pages: Go to Page Settings > Custom Code > Before </body> tag.
For site-wide forms: Go to Project Settings > Custom Code > Before </body> tag.
Depending on whether you want these rules to apply to a single page or the entire site, you can insert the code in the appropriate custom code area. The code to add is the following:
<script>
/**
* Custom form validation in Webflow
* Validate form inputs content
* @author BRIX Templates
* @version 1.0.0
**/
window.onload = function() {
const formValidators = [
{
id: '1',
// Block common personal email domains
blockedWords: ['@gmail.com', '@yahoo.com', '@hotmail.com', '@aol.com', '@outlook.com'],
errorMessage: 'Please use a professional email address. Personal email providers are not allowed.'
},
{
id: '2',
// Example: Block Russian domains or any other region-specific domains
blockedWords: ['@mail.ru', '@yandex.ru', '@bk.ru', '@list.ru', '.ru'],
errorMessage: 'We are not accepting submissions from these email domains. Please try another email.'
}
];
// Get error message element
const messageElement = document.getElementById('brix-form-validator-message');
// Remove any existing styles and classes
if (messageElement) {
messageElement.removeAttribute('style');
messageElement.classList.remove('show');
}
formValidators.forEach(validator => {
initializeValidator(validator);
});
function initializeValidator(validator) {
const inputField = document.getElementById(`brix-form-validator-${validator.id}`);
const submitButton = document.getElementById('brix-form-validator-button');
if (!inputField || !submitButton || !messageElement) {
return;
}
// Remove initial deactivated state
submitButton.classList.remove('deactivated');
inputField.addEventListener('input', function() {
const inputValue = this.value.toLowerCase();
const containsBlockedWord = validator.blockedWords.some(word =>
inputValue.includes(word.toLowerCase())
);
if (containsBlockedWord) {
// Handle error state
submitButton.disabled = true;
submitButton.classList.add('deactivated');
messageElement.textContent = validator.errorMessage;
messageElement.classList.add('show');
} else {
// Handle valid state
submitButton.disabled = false;
submitButton.classList.remove('deactivated');
messageElement.classList.remove('show');
}
});
}
}
</script>
Customizing the validation logic is both simple and flexible. Whether you’re just adding another blocked provider to an existing validator or establishing entirely new rules for a separate validator, you can easily adjust the script as following:
By implementing this custom form validation in Webflow, you take control of which email addresses can be submitted through your forms. Whether you’re aiming to block free, personal addresses to ensure more professional leads, or planning to restrict certain regions, this approach helps maintain data quality and reduce spam.
If you run into any problems while adding these features or need guidance on other Webflow integrations, just drop us a message. The BRIX Templates team is always ready to help keep things running smoothly.
Discover how to save time by automating lead transfers from Webflow to Klaviyo.
Learn how to automatically truncate long text in Webflow by adding a single custom attribute. 5-minute setup, no coding experience needed
Step-by-step guide to bypass Webflow's 3-field checkout limit. Learn how to add unlimited custom fields to your Webflow Ecommerce checkout.