I am working on a students' site and I only want people with emails ending to in ".ac.uk" to be able to register.

How do I go about this, so that any other email type will give error during registration

Thanks

Comments

rentex’s picture

You'd have to make a new module, unless you wanna hack the user-module.
Maybe there is allread a module out there that does this.

nevets’s picture

How to do it with a small module which is much prefered to hacking the user module. Create a directory under the modules directory called limitemail and place the following in a file call limitemail.module.

<?php

function limitemail_form_alter($form_id, &$form) {
	if ( $form_id == 'user_register' || $form_id == 'user_edit' ) {
		if ( $form['account']['mail'] ) {
			$form['account']['mail']['#validate'] = array('limitemail_validation' => array());
		}
	}
}

function limitemail_validation($element) {
	$mail = $element['#value'];
	$len = strlen($mail);
	if ( $len ) {
		$found = strstr($mail, '.ac.uk');
		if ( $found != '.ac.uk' ) {
			form_error($element, "The e-mail address must end in '.ac.uk'");
		}
	}
}

Enable the module and you should be be good to go.

droople’s picture

This didn't work here is the file path I set up /public_html/drupal/modules/limitemail/limitemail.module

I acivated the module as well

Please help

Thanks

nevets’s picture

When you say it does not work what does that mean?

droople’s picture

I still managed to register with a .com email address after going through that process

nevets’s picture

At the beginning of the validation function add a call to drupal_set_message(), something like this

drupal_set_message("Function entered");

to see if it is even entered. If not, you can do the same sort of thing in the form_alter hook to determine what code is or is not running.

droople’s picture

thanks but wow, I am a novice couldn't understand a thing there.

Could you put that in simple English if possible

Thanks mate

nevets’s picture

Here is a modified version of the module to let you determine what is going on

<?php

function limitemail_form_alter($form_id, &$form) {
drupal_set_message("limitemail_form_alter: entered, form id = $form_id");
if ( $form_id == 'user_register' || $form_id == 'user_edit' ) {
drupal_set_message("limitemail_form_alter: modify form?");
if ( $form['account']['mail'] ) {
drupal_set_message("limitemail_form_alter: mail field present modify");
$form['account']['mail']['#validate'] = array('limitemail_validation' => array());
}
}
}

function limitemail_validation($element) {
drupal_set_message("limitemail_validation: entered");
$mail = $element['#value'];
$len = strlen($mail);
if ( $len ) {
drupal_set_message("limitemail_validation: email set");
$found = strstr($mail, '.ac.uk');
drupal_set_message("limitemail_validation: found: $found");
if ( $found != '.ac.uk' ) {
form_error($element, "The e-mail address must end in '.ac.uk'");
}
}
drupal_set_message("limitemail_validation: returns");
}

All those calls to drupal_set_message will add output to the message error when you view/submit the user registration/edit form. At the very least you should see the output "limitemail_form_alter: entered, form id = ?" (where ? is a value). The question is which ones do you see, the ones for limitemail_form_alter should show when the form is display, the ones for limitemail_validation when the firm is submitted.