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.
I think we’d all benefit from a video on the backend and how to apply the code properly. I’m pretty well educated and even I’m not getting most of this. Why not have a huge H2 tag that says
HOW TO BAN!
and then another that says
HOW TO ALLOW
Even your plugin, like there’s nothing there at all.
Hi Echo,
Thanks for the feedback. We’ve started making usage videos for our snippet and Perks as mentioned here. Hopefully, there will be a video for this snippet soon. However, if you’re still experiencing issues setting this up, you can get in touch with us via our support form so we can assist you further.
Best,
Hello, is this still working?
I am recently getting an error:
The snippet has been deactivated due to an error on line : Cannot redeclare function str_ends_with.
I see that someone else has posted a comment regarding this recently as well.
Thanks
Hi Jay,
I’m unable to reproduce this when testing locally. If you have a Gravity Perks license, drop us a line and we’ll be happy to look into this with you.
Great work with this function.
Can it be extended to allow only fully email addresses such as: testuser@testemail.com
Thanks.
Hi Hayley,
If I understand correctly, you want to limit the form to only specific email addresses. If so, then this is not supported with this snippet. However, we have another snippet, which would work for what you’re trying to do. Please check out the documentation for the snippet and details of how it works. With this solution, you would store the email addresses as entries of another form and use that to set up the main form.
I hope this helps.
Best,
Hello,
Does not work for a few days!
Can not redeclary the STR_ENDS_WITH function.
Possible to correct please?
Thanks.
Hi Anderson,
When testing locally it seems to be working as expected.
If you have an active Gravity Perks License, you can get in touch with us via our support form with your account email address and we’ll be happy to dig into this further.
Best,
Hello! How I can multiple forms to form_id param?
Hey West, we have a handy tutorial written for this: https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/
TLDR; just create a new instance for each form. 🙂
Oh … finally figured out why I was getting a PHP fatal error every time I pasted the code into my functions.php file … I was copying and pasting the short snippet of code from where it says
“All you need to do is create a new instance of the GWEmailDomainControl() class, populated with your specific details, like so:”
But what I didn’t realise is that I had missed all the code hidden in the “Show Code” section!
Suggestion – could you make it clearer that there is a lot more code than what is shown in the post?
e.g. change this: How do I install this snippet? Easy peasy. Just copy and paste the code above into your theme’s functions.php file.
to this: How do I install this snippet? Easy peasy. Just click “show code” above, then copy and paste that code above into your theme’s functions.php file.
And change this: 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:
To this:
How do I use this functionality? After you have inserted the code above into your functions.php file, all you need to do is create a new instance of the GWEmailDomainControl() class, populated with your specific details, like so:
Hi Josh,
Thanks for the feedback. I’ll forward your suggestion for a proposal to review and update the document.
Best
Is this possible to have a wildcard of subdomain?
new GW_Email_Domain_Validator( array( ‘form_id’ => 2, ‘field_id’ => 4, ‘domains’ => array( ‘*.domain.com.au’ ), ‘mode’ => ‘limit’ ) );
Hi Steve,
Subdomains like this “domain.com.au” are actually supported. If the mode is set to limit, the snippet will only allow email addresses within the subdomain specified in the domain array parameter but you will exclude the wildcard and dot. Something like this should work
new GW_Email_Domain_Validator( array( 'form_id' => 2, 'field_id' => 4, 'domains' => array( 'domain.com.au' ), 'mode' => 'limit' ) );
Best,
Does this work on domains containing two dots? We have students who are on ‘student.organisation.tld’.
Hi Filip,
It should work with domains containing two dots.
Best,
Hello. I’ve used the code successfully for a single form. I wanted to use the same functionality for 3 similar forms with 3 different IDs. I tried using an array for the declaration and if statements that required the form id, but the “banned emails” did not filter. What should i do instead? Thank you so much!
I was thinking on copying all the codes 3 times for each form ID but that sounds like a bit of a hassle.
Hi Ana,
You can create multiple instances of the snippet configuration to target 3 different forms. Here is an article on how to do that: https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/
Best,
Hi Ana,
Did you find a solution? I have tried multiple variations on this and it seems to read/verify the last form entry.
Hi Carlos,
If you’re a Gravity Perks Subscriber, you can get in touch with us via our Support Form, with form exports and additional information on what you’re trying to achieve so we can assist you.
Best,
Is there a way to form this same function but instead for a single line text field?
Hi Mark,
That’s a great fit for GF Blacklist. You can use it to reject submissions for any field that matches a list of blocked words, phrases, or IPs.
So by just changing the field ID this function code will work with single text field inputs?
Hello Mark, You would need to change the form ID, Field ID and specify what domain you are looking to ban, then the functionality should work correctly for your use case.
this is brilliant, and thank you sincerely for it. I’ve created an admin form with a field in options table for a comma delimited list (e.g. domain.one, domain.two) – I want to use that list as my domains list. I have (via get_option) a string (domain.one, domain.two)… and in GWE array set ‘domains’ => $mystring. But it doesn’t work – ignore the domains entirely. I tried exploding the string to an arrray, still nothing. Any advice on how to get a string from WP_OPTIONS table to be the domains list? Even if not, thank you deeply for such a useful bit of code :)
Ok… I got it sorted… code in case it helps anyone.
https://snippi.com/s/yg7r42h
Cheers.