September 14, 2021 Migrated snippet to the Snippet Library.
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',
));
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 GW_Email_Domain_Validator( array(
'form_id' => 326,
'field_id' => 1,
'domains' => array( 'gmail.com', 'hotmail.com', '.co.uk' ),
'validation_message' => __( 'Oh no! <strong>%s</strong> email accounts are not eligible for this form.' ),
'mode' => 'limit',
) );
Parameters
form_id integer required
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 the
form_id
argument at all.field_id integer required
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 the
field_id
argument at all.domains array required
An array of email domains that should be banned or limited to depending on what
mode
you have set.validation_message string optional
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 string optional
By default, this is set to
ban
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 44+ essential add-ons for Gravity Forms with support you can count on.
Please forgive this question (I am not a developer), but is this script PHP or Javascript?
Hi Margaret,
This is a PHP snippet. Here’s documentation on how to install the snippet.
Best,
Hi great script , thank you
is there a way to have the validation error message show before the user presses submit? Maybe a listening action or listen to mutations as the email is typed?
Hi Tico,
Unfortunately, we do not have a live validation solution.
Best,
Ok thank you Bassah! Any ideas on modification approaches to the current code to achieve this or resources that i could reference?
Hi Tico,
Not at this time, unfortunately, but let us know if you find anything suitable so we can ping the team.
Best,
OK thanks
Would LOVE to see this as a Perk with easily configurable options in GF main settings to set domains or on a per-form / per email field basis. It would make implementing this so much easier! And you’d get paid for it!
Nice feature Rachel! Iโll escalate to our product manager and see what he thinks.
Love it, Rachel. ๐ฏ
Definitely something we’d like to pursue. We’ve made a few new hires in the last couple months and have plans to give some of our free snippets a good UI and bring them into the suite. ๐
Hello, I’m receiving the following error message:
“The snippet has been deactivated due to an error on line 104: Cannot redeclare function str_ends_with.”
Has anyone else received this error message? Any ideas on how to fix it ?
Thanks!
Hi Margaret,
It seems like a configuration error since the snippet works locally. If you have an active Gravity Perks Pro license, you can get in touch via our support form so we dig into this.
Best,
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.