September 14, 2021 Migrated snippet to the Snippet Library.
October 6, 2016: Fixed issue where top-level domains were not correctly validated. Props Robert!
May 24, 2016: Fixed issue where configured fields were validated on every page submission rather than only when the page that contains them was submitted.
September 30, 2015: Fixed issue where spaces after the email would cause validation to fail.
April 2, 2015: Fixed issue where if domain check was case-sensitive.
October 1, 2014: Added support for limiting by top-level domain (i.e. ".co.uk", ".edu", etc)
Want to ban users from entering test@test.com
and spamming your registration form? Want to only allow users with your companies email address to signup (i.e. jane@yourcompany.com). This snippet will let you do both!
How do I install this snippet?
Easy peasy. Just copy and paste the code above into your theme's functions.php file.
How do I use this functionality?
All you need to do is create a new instance of the GWEmailDomainControl()
class, populated with your specific details, like so:
new GWEmailDomainControl(array( | |
'form_id' => 152, | |
'field_id' => 9, | |
'domains' => array('gmail.com', 'hotmail.com', 'test.com') | |
)); |
By default, the form will “ban” the specified email domains; however, you can also limit by the specified domains by passing limit
for the mode
argument. The example below demonstrates this as well as how to apply the email domain control to two specific fields by passing an array of field IDs for the field_id
argument:
new GWEmailDomainControl(array( | |
'form_id' => 152, | |
'field_id' => 9, | |
'domains' => array('gmail.com', 'hotmail.com', 'test.com'), | |
'mode' => 'limit' | |
)); |
To apply this functionality to ALL fields on a form, do not pass the field_id
argument at all.
new GWEmailDomainControl(array( | |
'form_id' => 152, | |
'domains' => array('gmail.com', 'hotmail.com', 'test.com') | |
)); |
Here is a full list of available options:
new GWEmailDomainControl(array( | |
'form_id' => 152, | |
'field_id' => 9, // multiple field IDs can be passed as an array: array(8,9) | |
'domains' => array('gmail.com', 'hotmail.com', 'test.com'), | |
'validation_message' => __('Oh no! <strong>%s</strong> email accounts are not eligible for this form.'), | |
'mode' => 'ban' // also accepts "limit" | |
)); |
form_id
The form ID of the form you would like to apply this functionality to. If you want to apply the the same email domain control to all forms, simply do not pass theform_id
argument at all.field_id
The field ID for which this email domain control should be applied. If you would like to apply the same control to multiple fields you can pass an array of field IDs:'field_id' => array(8,12,15)
. If you want this to apply to all fields on a form, simply do not pass thefield_id
argument at all.domains
An array of email domains that should be banned or limited to depending on whatmode
you have set.validation_message
The message which will be displayed below the email field if the submitted email contains an invalid domain. You can use%s
anywhere in the message to output the invalid domain.mode
By default, this is set toban
which will mark any of the specified domains as invalid if they are submitted in the email field. You can also pass alimit
mode. In this mode, the specified domains are the only valid domains. All other domains will be marked as invalid.
Did this resource help you do something awesome with Gravity Forms? Then you'll absolutely love Gravity Perks; a suite of 39+ essential add-ons for Gravity Forms with support you can count on.
Hi,
The error is thrown when the user submits the form. Can we have the error displayed when the user navigates away from the field? That way we will have a better user experience and reduce the clicks for the customer and also the page reload will be saved too.
Hi Rupak,
That would have been nice but unfortunately, Gravity Forms doesn’t handle real-time validation by default. There are add-ons for Gravity Forms that add real-time validation, but they don’t typically support third-party field types.
I hope this helps.
Best,
Is there a way to edit the code to allow a specific email address, eg specificemail@gmail.com?
I have it locked down to only allow one domain but have need to add two specific emails that are not the same as the allowed domain.
Hi Daniel,
This should be possible with our GP Blacklist Perk. Please check it out.
If you’re having difficulties setting it up, you can send us a message via our support form and we’ll be happy to assist you further.
Best,
Great snippet – I’ve been using this for years but am now getting hit with a ton of spam from a wide variety of non-US TLDs….is it possible to enter a domain wildcard to ban all domains from a given TLD, something such as ‘*.ru’ ?
Hello Trisha, You should be able to do this by banning the domain like this: ‘domains’ => array( ‘.ru’ ); Hopefully that works for you.
Can I limit email address for multiple forms?! Like if u apply with one form u can’t apply for the other form with same Email address?!
Hello Othman, You can accomplish this by creating multiple instances of the snippet. More details here: https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/
How I can do the opposite in the domain option and have there only the emails allowed?
For example: I want to only accept emails from @mycompany.com
Any help will be much appreciated.
Hello Abel, We do have a snippet here that will do just that. Within the configuration you will want to change ban to limit so the form is limited to only emails from your companies emails.
passing more than one form ID only the first one validates, can I do multiple form IDs in the same script or do I need to a script for each??
THanks!
Hey Eric, you will need to create multiple instances of the snippet. More details here: https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/
Great snippet! Can this be used to ban domains with different country extensions without having to list every one? (for example yahoo.com, yahoo.de, yahoo.ca etc.)
Thanks!
Jonathan
Hi, your demo is still live and working properly. I added the code through the download code button in my child theme’s functions.php, altered the form ID and field ID (tried e-mail field – single and with double confirmation – and a simple text field) but nothing seemed to work.
Is your demo using the latest version of GravityForms?
Hello Jeroen,
I have just tested this out on my own test site and was able to use the snippet. Can you drop us a support request to better troubleshoot this for you? Thank you so much! :)
Hi David,
Does this snippet only work if we’re running Gravity Perks ad-ons?
Thanks!
Hi Lindsey, no, this should work with just Gravity Forms. I setup a demo so you can see it working: https://demos.gravitywiz.com/?gplp=preview&id=101
This snippet seems to be blocking all email addresses since Gravity Forms Version 2.4.14
Hi Dale, I setup a demo to see if I could recreate this issue but was unable to: https://demos.gravitywiz.com/?gplp=preview&id=101 If you’re a Gravity Perks customer, we’ll be happy to dig in to your implementation via support.
Hello,
Thank you for this! The functionality is working as intended but somehow it won’t show the validate messages anymore on the fields. I’ve used the validation you’ve created:
validation_message’ => __( ‘Sorry, %s email accounts are not eligible for this form.’ ),
Hi James, just setup a demo to see if I could recreate this issue. No luck: https://demos.gravitywiz.com/?gplp=preview&id=101
Hi there, any way to adjust this so it’s not a whole domain, but just a specific email address?
That’d be a better fit for our Gravity Forms Blacklist plugin. Alternately, you could just add conditional logic to the Submit button so that it’s hidden if the undesirable email is entered.
Hi David! Thanks for this snippet! It does exactly what I want. One thing I noticed in testing though which maybe you could help with…When I entered a banned domain and submitted the form (located at the bottom of my page), the page reloaded and stayed at the top. So a user would not know their form didn’t get submitted unless they scrolled down. Any thoughts on this?
It sounds like your issue is simply that the page isn’t scrolling down for any validation error. See the docs for gform_confirmation_anchor for some suggestions on this. ?
Hey David! Great code and works well in Gravity Forms. I implemented it and it stops entries from populating in the Gravity Forms area.
Something odd: my HubSpot integration is still picking up that I have a new entry from test@test.com and sends me an email to notify me when I submit the form.
Any tips to stop this from happening? Thanks!
Strange. I’d expect HubSpot’s feeds to only be triggered when the form is submitted (like other feed add-ons). If enough users report this issue, we’ll dig in and see if it’s something we can address in the snippet.
I’m having having a similar issue with hubspot. the validation catches the flagged email but the submission is sent to hubspot
I’m having trouble reproducing this with the Gravity Forms Hubspot Add-On. If you have a Gravity Perks subscription, drop us a line and we’ll be happy to dig into this.
Great snippet! Thanks a lot!
Glad to help, Karolina!
Hi. Is this still functioning? or is it dependent on anything else? The forms just continue to submit, unrestricted.
Hi Efficiency, yes, this is still working. There’s a chance you may not have installed it correctly. Try these troubleshooting steps: https://gravitywiz.com/documentation/snippet-troubleshooting/
Great snippet! Thanks so much for sharing :)
Glad to help. ?
Hi,
I want to translate the validation message with Loco Translate but for some reason it won’t translate the message.
Someone who have the same problem or a solution?
Thanks!
Can I just add this to ban email domains on ALL forms?
new GWEmailDomainControl(array( ‘domains’ => array(‘gmail.com’, ‘hotmail.com’, ‘test.com’) ));
Hi Paul, yes, that should work. :)
Hi there,
Does the ‘form_id’ accept an array as well? I only want to ban certain email domains on two of my forms…
Hi Caitlin, you will need to create multiple instances of the snippet. More details here: https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/