Ban/Limit Email Domains for Gravity Form Email Fields

This snippet allows you to ban or limit any email domain(s) for your Gravity Form email fields.

September 14, 2021 Migrated snippet to the Snippet Library.

September 14, 2021 Migrated snippet to the Snippet Library.

September 14, 2021 Migrated snippet to the Snippet Library.

September 14, 2021 Migrated snippet to the Snippet Library.

September 14, 2021 Migrated snippet to the Snippet Library.

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',
));
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 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 a limit mode. In this mode, the specified domains are the only valid domains. All other domains will be marked as invalid.

Comments

    1. Samuel Bassah
      Samuel Bassah Staff March 18, 2024 at 7:29 pm

      Hi Paul,

      It appears it also works for domain suffixes. The domain parameter of the configuration will be like so ‘domains’ => array( ‘.ru’, ‘.com’, ‘.net’ ).

      Best,

  1. jj
    jj October 11, 2023 at 12:47 pm

    Hi there

    Great snippet indeed,

    In my use case, I have a select field, which get some wp-users, and the field value is the selected user email.

    Then I have an email field in my form, and I would like to chek if the email domain in this field is the same domain than the select field value.

    If yes, then some field are displayed, if no some other field are displayed.

    Is there any way to acheive this with this snippet or any mean to do that anyhow ?

    Thank’s

    Reply
    1. Scott Ryer
      Scott Ryer Staff October 11, 2023 at 2:13 pm

      You could do this by combining GP Copy Cat with this snippet. The snippet adds support for comparing two field’s values to trigger conditional logic.

      Basically, you’d add two hidden Text fields to your form and use Copy Cat to copy the domains. Then, you’ll use conditional logic to compare those copied values and trigger the conditional logic. You’ll need an additional snippet to modify Copy Cat’s copied values. If you’re a Gravity Perks customer, drop us a line and we’ll be happy to help.

  2. Paul @ Forest Design
    Paul @ Forest Design July 18, 2023 at 11:05 am

    This is really useful, there are a few individual people (or bots or whatever) that incessantly spam me and it’s great that I can filter them out. THanks!

    Reply
  3. Tico
    Tico October 7, 2022 at 5:49 pm

    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?

    Reply
    1. Tico
      Tico October 11, 2022 at 1:00 pm

      Ok thank you Bassah! Any ideas on modification approaches to the current code to achieve this or resources that i could reference?

    2. Dario Space
      Dario Space October 11, 2022 at 1:16 pm

      Hi Tico,

      Not at this time, unfortunately, but let us know if you find anything suitable so we can ping the team.

      Best,

  4. Rachel Cardy
    Rachel Cardy September 7, 2022 at 2:20 pm

    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!

    Reply
    1. David Smith
      David Smith Staff September 7, 2022 at 5:05 pm

      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. 🙂

  5. Margaret
    Margaret June 13, 2022 at 11:08 am

    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!

    Reply
    1. Dario Space
      Dario Space June 13, 2022 at 11:28 am

      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,

  6. Echo
    Echo February 3, 2022 at 8:24 pm

    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.

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff February 8, 2022 at 4:40 am

      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,

  7. Jay
    Jay October 18, 2021 at 5:22 pm

    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

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff October 7, 2021 at 7:25 am

      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,

    1. Dario Space
      Dario Space September 10, 2021 at 11:01 am

      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,

  8. Josh Moore
    Josh Moore May 31, 2021 at 1:27 am

    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:

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff May 31, 2021 at 6:40 am

      Hi Josh,

      Thanks for the feedback. I’ll forward your suggestion for a proposal to review and update the document.

      Best

  9. Steve
    Steve April 19, 2021 at 9:34 pm

    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’ ) );

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff April 20, 2021 at 8:12 am

      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,

  10. Ana Zamora
    Ana Zamora February 5, 2021 at 1:56 am

    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!

    Reply
    1. Ana Zamora
      Ana Zamora February 5, 2021 at 1:58 am

      I was thinking on copying all the codes 3 times for each form ID but that sounds like a bit of a hassle.

    2. Carlos
      Carlos February 24, 2021 at 6:53 pm

      Hi Ana,

      Did you find a solution? I have tried multiple variations on this and it seems to read/verify the last form entry.

    3. Samuel Bassah
      Samuel Bassah Staff February 25, 2021 at 6:05 am

      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,

    1. Scott Ryer
      Scott Ryer Staff January 20, 2021 at 1:37 pm

      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.

    2. Ryan Donovan
      Ryan Donovan January 22, 2021 at 2:24 pm

      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.

  11. This Chap
    This Chap January 15, 2021 at 7:03 am

    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 :)

    Reply
  12. Rupak
    Rupak January 6, 2021 at 2:16 am

    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.

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff January 6, 2021 at 11:05 am

      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,

    1. Samuel Bassah
      Samuel Bassah Staff November 3, 2020 at 6:48 am

      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,

  13. Trisha
    Trisha September 26, 2020 at 12:21 pm

    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’ ?

    Reply
    1. Ryan Donovan
      Ryan Donovan September 26, 2020 at 8:11 pm

      Hello Trisha, You should be able to do this by banning the domain like this: ‘domains’ => array( ‘.ru’ ); Hopefully that works for you.

  14. Othman
    Othman August 22, 2020 at 6:39 pm

    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?!

    Reply
  15. Abel Valdivia
    Abel Valdivia August 13, 2020 at 12:39 pm

    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.

    Reply
    1. Ryan Donovan
      Ryan Donovan August 13, 2020 at 12:53 pm

      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.

  16. Eric
    Eric May 12, 2020 at 11:03 pm

    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!

    Reply
  17. Jonathan Timms
    Jonathan Timms March 28, 2020 at 11:56 am

    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

    Reply
  18. Jeroen Sterk
    Jeroen Sterk January 23, 2020 at 5:34 am

    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?

    Reply
    1. Ryan Donovan
      Ryan Donovan February 14, 2020 at 12:37 pm

      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! :)

  19. James
    James October 2, 2019 at 8:16 am

    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.’ ),

    Reply
  20. Brett C.
    Brett C. July 23, 2019 at 4:42 pm

    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?

    Reply
  21. Ryan
    Ryan June 3, 2019 at 10:13 am

    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!

    Reply
    1. David Smith
      David Smith Staff June 3, 2019 at 3:25 pm

      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.

    2. Mohamed Hamad
      Mohamed Hamad April 23, 2020 at 3:10 pm

      I’m having having a similar issue with hubspot. the validation catches the flagged email but the submission is sent to hubspot

  22. Efishinsea
    Efishinsea May 9, 2019 at 6:48 pm

    Hi. Is this still functioning? or is it dependent on anything else? The forms just continue to submit, unrestricted.

    Reply
  23. bjornvr
    bjornvr November 14, 2018 at 3:09 am

    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!

    Reply
  24. Paul O'Brien
    Paul O'Brien July 31, 2018 at 2:14 pm

    Can I just add this to ban email domains on ALL forms?

    new GWEmailDomainControl(array( ‘domains’ => array(‘gmail.com’, ‘hotmail.com’, ‘test.com’) ));

    Reply
  25. Caitlin
    Caitlin June 24, 2018 at 5:22 pm

    Hi there,

    Does the ‘form_id’ accept an array as well? I only want to ban certain email domains on two of my forms…

    Reply
    1. David Smith
      David Smith Staff May 4, 2018 at 4:44 pm

      This will not ban specific email addresses; however, you could do that with GP Blacklist. Just add the email as a blacklisted word and enable the blacklist on your email field.

  26. Sherrie
    Sherrie February 21, 2018 at 8:51 pm

    Hey,

    Say I have 10 forms and i just want the validation to work on 5 of them, i tried array with “form_id” but it doesn’t seems to work. Any advice?

    Reply
    1. Derrick
      Derrick October 19, 2017 at 4:18 pm

      Thanks David. So do I use an asterisk or something? Like @.ru? Just to be clear I want to ban all .ru emails. Thanks in advance?

    2. Davide
      Davide November 8, 2017 at 4:15 am

      Hi David,

      great snippet, perfect to block mail.ru spammers sponsoring ‘SibCoin’ (russian bitcoin) – I’m receiving hundreds of these requests.

      Do you have a similar snippet to block spam / bad words (e.g. ‘SibCoin’, …) instead of email domains?

      Thanks

  27. Aamir
    Aamir July 19, 2017 at 6:48 am

    why this mess while we can use ‘gform_field_validation’ filter i am doing the same thing with in 8 lines of code

    Reply
  28. Mike
    Mike July 18, 2017 at 4:47 pm

    For me, I noticed the two checks between lines 50-57 proceed onto the next field even when “email” and “field_id” conditions are met, preventing the final validation step of the domain(s).

    commenting/removing these lines is a hotfix if all you’re concerned with are the field type and id, but perhaps a different approach that covers all conditions..?

    Reply
    1. David Smith
      David Smith Staff July 18, 2017 at 5:18 pm

      Hi Mike, I’m not sure I follow? What are you trying to do and how is the current code failing?

  29. Michael
    Michael June 28, 2017 at 7:28 am

    Hi, thank you for your work. Can You help me please? I need to people only with gmail and hotmail account could subscribe. So I’ve used you code. But for some reason I have an error. “Parse error: syntax error, unexpected ”field_id” (T_CONSTANT_ENCAPSED_STRING), expecting ‘)’ in /var/www/html/wp-content/themes/charitas-wpl/functions.php on line 114″ But my field id is right. I’ve tried to delete string with field id, to apply it to all fields. But then: “Parse error: syntax error, unexpected ”domains” (T_CONSTANT_ENCAPSED_STRING), expecting ‘)’ in /var/www/html/wp-content/themes/charitas-wpl/functions.php on line 114″

    Can you help me please? What’s wrong?

    Reply
  30. Jeffrey A Goodman
    Jeffrey A Goodman June 19, 2017 at 9:31 am

    Thank you for this article. I came upon it as I have come to discover that when people enter their email @ymail.com that the form does not get sent to notify me. I am not clear how to apply what you have here to fix this. I have tried testing out the form entering all types of @bmail @ fmail and all work but not @ymail is this something you can help me with?

    Reply
    1. David Smith
      David Smith Staff June 20, 2017 at 8:54 am

      Is your goal to ban @ymail.com email addresses? Can you share how you’ve configured the snippet (just the configuration at the bottom)?

    2. Jeffrey A Goodman
      Jeffrey A Goodman June 20, 2017 at 9:20 am

      I did not put the snippet in.

      I noticed that when someone is using a ymail. It does not get sent to me. So I began looking to see how to fix or if this is a glitch. I then came across this post but I am not sure how to use it in a way to fix this issue.

      I do not want to ban ymail. I want them to be sent to me.

      Here is the whole code for the function.php

    3. David Smith
      David Smith Staff June 20, 2017 at 10:39 am

      Hi Jeff, I don’t think this snippet is going to help you. It sounds like there is some sort of mail server issue. Perhaps your mail server is interpreting ymail.com addresses as spam?

  31. Jason
    Jason April 30, 2017 at 10:37 pm

    I seem to be having issues getting it working. Does this NEED to be in the theme functions? I added the code in a plugin because i am in the process of cleaning up the clients site and the theme needs to be cleaned out to allow a child theme to be created.

    When i add the basic code i can still register with banned email accounts.

    Reply
  32. Marc
    Marc January 12, 2017 at 3:40 pm

    I was wondering if anyone knows of a list of the top 100 or so types of free email clients (like gmail, yahoo, ymail, aol and so on) so we could limit our requests from these bulk spammers?

    Any links or anything would be greatly appreciated :)

    Reply
  33. Britt
    Britt November 2, 2016 at 12:34 pm

    Does this code snippet apply to one specific email address (as opposed to blanketing a domain)? I have a specific person spamming my contact form daily and I just want to block that one email address.

    Reply
    1. David Smith
      David Smith Staff November 3, 2016 at 1:13 am

      Hi Britt, this does not currently support blocking a single email address. You could add conditional logic on your submit button though that hides the submit button if that specific email address is entered.

  34. Dan Greene
    Dan Greene October 14, 2016 at 10:25 am

    If you’re using WordPress Multisite is there a way to make this only work on forms within a specific site? By using site id in the array maybe?

    Reply
    1. Dan Greene
      Dan Greene October 26, 2016 at 2:42 pm

      Thanks David. I’ll see if that works. Instead of putting this in a theme’s functions.php couldn’t it also be implemented as a plugin?

  35. Robert
    Robert October 6, 2016 at 12:31 pm

    I believe your code has a typo at line 95. I believe you were wanting to compare the arguments domain ($_domain) instead of the email domain in order to limit/ban all emails from a TLD.

    Reply
    1. David Smith
      David Smith Staff October 6, 2016 at 9:59 pm

      Hey Brian, do you have a use-case of a domain that’s failing? I just tested on my local form and the code appears to be working as expected.

    2. Robert
      Robert October 6, 2016 at 10:40 pm

      I was trying to ban a top level domain. The strpos function is looking for a “.” as the first character but referencing the domain within the form field but should reference the arguments array domain ($domain). I added the “” and it worked perfectly.

  36. Simon Millard
    Simon Millard September 1, 2016 at 6:08 am

    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

    Reply
    1. Simon Millard
      Simon Millard September 1, 2016 at 10:32 am

      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

    2. Simon Millard
      Simon Millard September 2, 2016 at 8:36 am

      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.

    1. David Smith
      David Smith Staff August 8, 2016 at 2:46 pm

      Are you trying to limit to only use the specified domains or ban the specified domains?

  37. David Boroi
    David Boroi July 17, 2016 at 9:27 am

    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

    Reply
    1. David Smith
      David Smith Staff June 6, 2016 at 9:32 am

      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!

  38. Casey Perno
    Casey Perno May 31, 2016 at 1:57 pm

    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!

    Reply
  39. David Wilson
    David Wilson May 30, 2016 at 11:07 pm

    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.

    Reply
  40. Kevin Adams
    Kevin Adams May 26, 2016 at 11:47 am

    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!

    Reply
  41. Mr Gnome
    Mr Gnome May 14, 2016 at 10:30 am

    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!

    Reply
  42. David
    David March 22, 2016 at 3:27 am

    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.

    Reply
  43. Hugh Heinsohn
    Hugh Heinsohn December 3, 2015 at 3:03 pm

    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]

    Reply
  44. Josh Jenkins
    Josh Jenkins October 27, 2015 at 12:45 pm

    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?

    Reply
    1. Josh
      Josh October 27, 2015 at 1:55 pm

      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.

    1. David Smith
      David Smith Staff September 28, 2015 at 8:15 am

      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.

  45. Stefan Lesik
    Stefan Lesik May 5, 2015 at 6:50 am

    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

    Reply
    1. Stefan Lesik
      Stefan Lesik May 5, 2015 at 7:15 am

      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!

    2. David Smith
      David Smith Staff May 5, 2015 at 7:18 am

      Ah, ok. Unfortunately, this snippet won’t do that for you. It only bans or limits which email addresses can be submitted.

    1. David Smith
      David Smith Staff April 30, 2015 at 9:46 pm

      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?

  46. Janet Kirklen
    Janet Kirklen April 2, 2015 at 1:19 pm

    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?

    Reply
    1. Janet Kirklen
      Janet Kirklen April 2, 2015 at 3:08 pm

      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));

    2. David Smith
      David Smith Staff April 2, 2015 at 10:14 pm

      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.

  47. hannah
    hannah March 4, 2015 at 6:49 pm

    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?

    Reply
  48. Stefan Lesik
    Stefan Lesik December 23, 2014 at 7:29 am

    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?

    Reply
  49. Adam
    Adam October 1, 2014 at 7:12 pm

    Gidday David, thank you for this great little snippet.

    I have added Gmail as an allowed domain, but the script also allows submissions from @xyzgmail.com. Is it possible for validation to be performed against the exact domain rather than a partial match?

    Reply
    1. Adam
      Adam October 2, 2014 at 4:21 am

      Goodness, that was super fast! Many thanks for the updated snippet and everything you’re doing with Gravity Perks. Cheers again.

  50. Pixeled Media
    Pixeled Media August 31, 2014 at 11:25 pm

    Hi there David,

    How do you only allow emails with a specific TLD (top level domain)?

    For example, I only want the form to accept email addresses ending in “co.fr” or “co.uk”.

    Is that possible? I posted a similar comment earlier but I don’t know what happened to it.

    Thanks!

    Reply
    1. Lewis Pixeled
      Lewis Pixeled September 2, 2014 at 7:33 pm

      Actually on more feature request:

      For example, here is an example of an email address that I want to allow:

      “sub2.sub1.domain.co.uk”

      This snippet works perfectly, but how do I limit email address with at 1, 2 or 3 subdomains, and still limit the form to only accept email addresses ending in “co.uk” for example?

    2. David Smith
      David Smith Staff September 3, 2014 at 12:34 pm

      This request is pretty specific. I’m going to hold off and see if any other users need something like this. If so, I’ll be happy to take a look at adding support for it.

  51. John
    John August 31, 2014 at 6:44 am

    Hi David,

    Thank you for this valuable and helpful feature! Are we free to use for commercial use in the purchased Gravity Forms plugin? Thanks.

    Reply
  52. Arpita
    Arpita July 13, 2014 at 11:42 pm

    I have followed the instructions and added this code to my functions.php. I have changed all instances of ban to limit as I want to allow only emails ending in mydomain.org to register. I have added this to the domains array. Unfortunately it still allows registration with any email address domain. Have I missed something? Is there something else that needs changing to make this work?

    Please reply ASAP.

    Thanks, Arpita

    Reply
  53. AndyC
    AndyC July 9, 2014 at 2:50 am

    Thanks for this! I am trying to get this to work with the Gravity forms User Registration plugin.

    I have changed your code line 26 from:

    add_filter(“gform_validation{$form_filter}”, array($this, ‘validate’));

    to:

    add_filter(“gform_user_registration_validation($form_filter)”, array($this, ‘validate’));

    When calling GWEmailDomainControl() class in the array data I am specifying the form and field id of the gravity forms form linked to the User Registration settings.

    Has anyone had experience applying this to User Registration forms?

    Thanks!

    Reply
  54. Jeremy Tyarks
    Jeremy Tyarks June 4, 2014 at 1:24 pm

    Hey David,

    Does this class allow for a dynamic array of domains? I have a project where the domains need to be determined from within WordPress rather than so I created a custom taxonomy that features a custom meta field for domain names, however the class doesn’t seem to accept said values. Instead it returns the following error:

    Warning: in_array() expects parameter 2 to be array, null given in /public_html/wp-content/themes/themename/inc/theme-functions.php on line 150

    I’ve confirmed that it does work with a hardcoded array of domains, so I’m sure I didn’t make a mistake copying over your code and I’ve tested my function in a few templates and it returns values as expected. (So, I’m fairly certain I didn’t make a silly PHP mistake. Maybe you’ll see something that says otherwise.)

    Anyway, here’s an example of the code I’m using to create the array of domains: http://pastie.org/private/x6llxgyfpku9yl0cxk6yq

    Note: Removing the else statement from get_brand_domains() does not change the warning, which I found interesting.

    Any thoughts on this? Thank you so much!

    Reply
    1. David Smith
      David Smith Staff June 4, 2014 at 9:55 pm

      Hi Jeremy, what output do you see if you print_r( $whitelist ); (anywhere below line 26)? My guess is that you aren’t getting an array of terms and instead you’re getting the “null” return (line 21). This may be caused because custom taxonomies have not yet been initialized when the functions.php is being loaded. If this is the case, you can wrap the initialization code in a function triggered on the “init” action: http://pastie.org/private/bar2edkgewb86xhnzcma

    2. Jeremy Tyarks
      Jeremy Tyarks June 5, 2014 at 1:33 am

      Ahh, you guessed correctly–it was due to the custom taxonomy not being initialized. So, not a silly PHP error, but a silly WordPress error. My apologies for bugging you but thank you so much for your time and expertise; both are very much appreciated.

      Cheers!

  55. Charlie
    Charlie April 24, 2014 at 12:34 pm

    Hi David,

    I need to create a similar function that allows me to block all domains from a particular TLD for example all .tk email addresses?

    Is there anyway to edit your function to allow this?

    Thanks for any help

    Charlie

    Reply
  56. Scot MacDonald
    Scot MacDonald March 25, 2014 at 3:16 pm

    David

    I’ve tries this on a current version of WP and GF but I get a white screen error. I’ve only changed the field IDs so not sure what the issue is. Any sugegstions?

    Reply
  57. Jon Anderson
    Jon Anderson December 30, 2013 at 12:50 pm

    Hi David,

    Trying to use this snippet with GF 1.7.2 but it generates a fatal error. What am I missing? Any help would be much appreciated!

    Here’s the code I’m trying to use in the functions.php file:

    //ban spammy email domains from gravity forms new GWEmailDomainControl(array( ‘domains’ => array(’email.com’, ‘asdf.com’, ‘test.com’) ));

    Reply
    1. David Smith
      David Smith Staff December 30, 2013 at 9:56 pm

      Thanks for the email update Jon! For others with the same issue, Jon was able to resolve his issue by re-pasting the full snippet.

  58. Tyler
    Tyler December 4, 2013 at 12:56 pm

    Any way to get this to ban a specific email (i.e. email@gmail.com) instead of all gmail accounts. I have a random gmail address sending an email through about 10 times a day but I don’t want to ban all gmail addresses.

    Reply
    1. David Smith
      David Smith Staff December 4, 2013 at 2:18 pm

      Hi Tyler, here’s a non-obvious solution that might do the trick.

      1. Add a Single Line Text field.
      2. Mark this field as required.
      3. Add a CSS class on the Advanced Settings tab for this field: “gf_hidden”.
      4. Enable conditional logic on this field. “Show this field if [Email field] [is] [email@gmail.com]”.

      What will happen now is when the “email@gmail.com” email is entered in the Email field, our required Single Line Text field will be “enabled” via conditional logic (though it will still be invisible, hidden by the “gf_hidden” CSS class) so when the form is submitted, it will fail validation and the user will be unable to submit unless they enter a different email address.

  59. Jason
    Jason October 29, 2013 at 3:55 pm

    Perfect! Thank you David! This is great for those that want to prevent spam from temporary email addresses; such as sharklasers.com, guerrillamail.com and 10minutemail.com domains.

    Reply
  60. Colin
    Colin October 24, 2013 at 7:02 am

    I have followed the instructions and added this code to my functions.php. I have changed all instances of ban to limit as I want to allow only emails ending in mydomain.org to register. I have added this to the domains array. Unfortunately it still allows registration with any email address domain. Have I missed something? Is there something else that needs changing to make this work?

    Reply
  61. sofie
    sofie July 10, 2013 at 3:54 am

    Hi David,

    Can this be done in another way :

    • blocking free email accounts is the standard
    • allowing free email accounts by changing the code.

    Would be nice

    Kind regards,

    S

    Reply
  62. yves
    yves May 12, 2013 at 5:37 pm

    Hi David,

    Thank you for sharing this. Can you help me with the following?

    On my website I have several forms. Some of them are only for business others for both business and personal use.

    I would like to avoid gmail and hotmail on the “business forms”. So far no problem.

    But we need to create several forms (+260 forms) and I would like to avoid to change the functions.php every time a form created. Is it possible to address your code directly in the form editor of GF? In that case blocking email domains for a certain form is just so easy.

    Or in case the first is not possible… We have more “business forms” than “both business and personal forms”. So your snippet is actually the standard for most forms. Can we instead of adding form IDs ‘form_id’ => 152, 153,… using negative form IDs to exclude them ‘form_id’ => -9, -10

    Thank you for your time and consideration.

    Kind regards

    Reply
    1. yves
      yves May 19, 2013 at 4:11 pm

      Hi David,

      Coming back to my question above… Still hope you have time to help…

      Can you change your snippet in a way that an array can be used for the form_id.

      Kind regards,

      Y

  63. Avinash D'Souza
    Avinash D'Souza January 16, 2013 at 6:55 am

    This is gonna sound a bit stupid but how do I “create a new instance of the GWEmailDomainControl()class”?

    Does that mean the second snippet gets pasted into functions.php too?

    Reply
    1. David Smith
      David Smith Staff January 16, 2013 at 8:07 am

      Hi Avinash, all you need to create a new instance of the GWEmailDomainControl() class is to use this initializer:

      http://pastie.org/5695188

      You can create as many initializations of the GWEmailDomainControl() class as you need. :)

  64. David Wang
    David Wang January 4, 2013 at 10:57 am

    Hey David you are awesome! I’m still a PHP noob but I’m guessing from the code that I can simply declare more

    new GWEmailDomainControl

    to apply the validation to a 2nd, 3rd etc form right?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.

Download Snippet

Ban/Limit Email Domains for Gravity Form Email Fields

This field is for validation purposes and should be left unchanged.