I have been tasked with blocking specific email address domians from registering at our Drupal 6 site (gmail.com, aol.com & yahoo.com). I implemented some JQuery code to do this but the problem was that spammers with javascript disabled in their browser readily bypassed this prevention.

I also cannot use Drupal 6's Access Rules as we already have registered users on the site with those domains so this method blocks them from logging in so it's more of a going forward prevention that we would like to implement.

I found this code on Stack Overflow but the code was somewhat incomplete:

function example_data_form_user_register_alter(&$form,&$form_state) {
  $form['mail']['#element_validate'] = 'example_mail_validate';

function example_mail_validate($element, &$form_state) {
   if ( -- Whatever condition you like -- ) {
     form_error($element, t('Free email is not allowed.'));

I thought it would be better to open a new question on this forum since it's Drupal specific.

I think what I need help with is filling in the gap where in the code it says: -- Whatever condition you like --

I am not a PHP guru so any help would be appreciated. I assume this is the spot where I would have some code that disallows the domains that I want to block.

1 Answer 1


A very simple example would be:

function example_mail_form_user_register_alter(&$form,&$form_state) {
  $form['mail']['#element_validate'] = array('example_mail_validate');

function example_mail_validate($element, &$form_state) {
   //block gmail addresses
   if (stripos($form_state['values']['mail'], 'gmail.com') > 0) {
     form_set_error('mail', t('Free email is not allowed.'));

However you should lookout for more advanced solutions:

  • Simple email verification: email verify
  • http:BL

    http:BL can prevent email address harvesters and comment spammers from visiting your site by using a centralized DNS blacklist.

  • Mollom which also prevent spam on all your content/comments forms.

  • Thanks for all the links, I will check those out. I created a custom module from the code above and substituted my module name for "example", activated the module and cleared my cache. However, it did not work. I have a pastebin started to see if we can work on the code a little further. pastebin.com/0C6LbpFK -- thanks. Jan 6, 2012 at 1:34
  • 1
    Your problem is most likely what is mentioned in your code comment. The code "array('prevent_emails_mail_validate');" should refer to the function below, which is now called: prevent_emails_email_validate. The extra e means drupal doesn't find the function, and validation fails.
    – Letharion
    Jan 6, 2012 at 7:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.