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 46+ essential add-ons for Gravity Forms with support you can count on.
BRILLIANT! Fixed, thank you so much.
Hi,
This is a great snippet and worked as expected so thank you for providing this!
Since our launch we found that country India has to be excluded from the free domain validator as India Universities do not have university specific email addresses for students. So I created a conditional logic which opens a second email field when country India is selected. Because we pre-populate some fields, it is validating the original field now hidden.
What would be the best way to only validate when it is an active field or exclude Country of Academic institution: India?
Link: https://qlikid-qa.qlik.com/signin?returnURL=https://qlikacademicprogram.qlik.com/student-application-form Username: amgreen Password: Sense2016
Form fields: Country of academic institution: India has no restriction and changes the email field. but the validator still errors for the other countries.
Thanks for your support.
Simon
Give this updated version a try: https://gist.github.com/spivurno/4055158
David, it works perfectly now! That is insane, you solved that in a matter of minutes, Awesome to have your talent and put it to great use! It really is so much appreciated, I really can’t believe how quick it was fixed!
Simon
Hi David,
Sorry to be a pain, using this alongside : https://gist.github.com/spivurno/7029518 if I enter a banned email which prevents progressing then the fields to field snippet picks up the first entry made(banned email), not the corrected valid email. I wonder if this is because of multi-page form: https://qlikacademicprogram.qlik.com/student-application-form/
If you happen to be able to help that would be superb.
Thanks Simon
P.S. Submitting here also as the other page does not seem to have activity anymore.
Try using the “force” modifier on your merge tag: {Field Label:12:force}
Hello, Thanks for the code, but somehow I cannot get this working! Here is the url: http://livetiles.wpengine.com/blog/big-data-analysis-new-big-thing/ Here is the pastie: http://pastie.org/10920724 I’d really appreciate if you can help me :) Thanks
Are you trying to limit to only use the specified domains or ban the specified domains?
Wow thanks so much! Works perfectly!
Glad you found this useful, Steve. :)
I get the following error: Fatal error: Class ‘GWEmailDomainControl’ not found in public_html/wp-content/themes/Avada-Child-Theme/functions.php on line 1187
Hi David, did you include the full snippet and not just the bit that initializes it?
Has anyone done something like this built into the email address field and using something like https://github.com/mailgun/validator-demo that would be awesome!
Not yet but we’re excited about a new perk that is coming soon with support for this! It’s going to complement one of our other upcoming perks really well, GP Page Transitions. Stay in the loop!
Hi,
Thanks for this neat snippet! Unless there was something funky in my environment, I think you introduced a problem for single-page forms when you added support for multi-page forms. I kept getting a undefined function on line 50 ( self::get_source_page( $form[‘id’] ) ), but once I commented out lines 50- 53 it worked fine.
Thanks again!
Hi Casey, you are correct and this has been fixed. :)
Hello,
I have pasted the code, removed the double opening tag, changed the form and field id, as well as the validation message – but I’m getting a http error 500.
Here is my entire child theme php (small amount of other code at the top, followed by your code): http://pastie.org/10858704
Any ideas?
Thank you in advance.
Try again with the latest version of the snippet (above). It has been updated.
I have tried installing this snippet several times and I keep getting the following error
“Fatal error: Call to undefined method GW_Email_Domain_Validator::get_source_page() in /home/serverdirectorhere/public_html/sitenamehere/wp-content/themes/customthemename-2015-Theme/functions.php on line 119”
Any suggestions would be greatly appreciated, thank you!
This issue has been fixed.
This is such a great feature but sadly I can’t get it to work on my multi-page form. The form is set up so that the email is the last field on the third page. It is also dynamically populated via a url parameter, so that it is pre-populated.
So when a user is on the first page and click on the next button, the form throws an error and won’t let them proceed.
If you could point me in the right direction to fixing this I’d greatly appreciate it. Thanks in advance!
Issue fixed. Grab the latest version above. :)
Thank you David, fixed now.
Hi David,
I tried using the code above and when I press “Submit”, the page just hangs. It doesn’t validate nor pass any information to the system and I’m getting a functions.php error: “Parse error: syntax error, unexpected ‘)’ after pasting the code and modifying it (to limit specific domains).
Thanks a lot for this solution. I hope you can help us out as to what went wrong.
Site: delseyb2b.asia
Pastie here – http://pastie.org/10769455
Error on line 177
Cheers
Looks like you added an extra two closing parenthesis. I’ve highlighted the line to remove: http://pastie.org/10769455#173
Thanks David, fixed.
I have installed the code successfully as part of my functions.pho file on this staging site:
http://thinktank01.staging.wpengine.com/
However, it’s not working – I can still enter gmail and hotmail addresses in the Gravity forms.
I can’t see anything I might be doing wrong but obviously I don’t have something set up correctly.
Here is the code I have appended to the functions.php file:
[code removed]
Hi Huge, could you share a pastie of your code? The formatting of the comments makes it difficult to read.
Trying out this snippet, I get a fatal error that php can’t redeclare the GWEmailDomainControl class. I’ve followed the tutorials, tried everything that my limited knowledge of php will allow, but no dice on a fix.
Any ideas?
Hi Josh, this means you’re including the snippet multiple times, possibly in the same file. Try removing the snippet completely and installing with these instructions: https://gravitywiz.com/documentation/snippet-troubleshooting/
Thanks so much for the quick response. I had actually already included the class for another project a year ago and totally forgot it was being called! Oops.
Thanks again.
Aha! Glad you were able to get this sorted. :)
is there a way to ban one specific email address? i.e instead of gmail.com can we singularly ban joe@gmail.com?
Hi Pete, this does not support banning a specific email address. It might be something I’ll add support for in the future. For now, there are two ways I can see doing this without custom code (depending on your needs).
1 – Enable “No Duplicates” on the email field and manually submit an entry with the email you wish to ban. The “No Duplicates” restriction will prevent this email from being submitted again.
2 – Use GP Comment Blacklist. Add the email you want to ban as a blacklisted phrase and enable the blacklist validation for this field.
I’d like to append a value to another field when a banned email address is entered. Eg. add ‘banned ermail’ to input_1_3 as well as banned validation message. How do I do this? Thanks
Could you elaborate on what you’re trying to accomplish?
Sorry I wasn’t clear in my previous comment.
When a banned email is entered and the form is submitted, instead of showing a validation message, I still want the form to be submitted but append a value to a hidden field instead. The hidden field will have a default value of 1 but the value will be replaced with 2 if a banned email is entered. I can then use the value of this hidden field to create some conditional logic, so if value = 1 redirect to page a if value = 2 redirect to page b. Hope that makes sense!
Ah, ok. Unfortunately, this snippet won’t do that for you. It only bans or limits which email addresses can be submitted.
Hi This is my bottom code http://pastie.org/10122990 but that seem not working , I believe the new GWEmailDomainControl has been put in a wrong way, please verify
It looks like your initializing this twice for the same field with different parameters. I’d remove this bit (highlighted). If it’s still not working, can you describe how it is not working and provide a link where I can preview the form?
Oh that did the tweak , Thanks man
I’m having an issue with case sensitivity. If my domains array includes “gmail.com” and the form input value is “Gmail.com” then how can I be sure that the email is converted to lowercase before GWEMailDomainControl does its compare?
To fix the case sensitivity I’ve changed line 50 in the snippet:
$domain = $this->get_email_domain($field);
to this:
$domain = strtolower($this->get_email_domain($field));
Hi Janet, thanks. I’ve updated the snippet to also account for the scenario where the admin user may specify the email domains to limit in uppercase or lowercase as well. It will now lowercase both values before comparison.
Hey
Great concept – just wish I could use it. I tried using it on our website but unfortunately it gave me the white screen of death – eeek! I am using x theme with a child theme on top.
Any ideas why?
Hi Hannah, this is generally caused by installation issues. Here’s a guide to help with common problems:
https://gravitywiz.com/documentation/snippet-troubleshooting/
Just what I’m looking for! Thanks!
I’d like to ban all popular webmail domains – gmail, yahoo etc. Is it possible to add a wildcard instead having to add each suffix? so gmail.* for example?
Hi Stefan, currently this is not possible. You’d need to specify each domain manually.