I need to compare the text in the username field with the text in email field of the registration form. If they are not equal i want to stop the registration. I think i should use a hook but i really don't know where to start from.

Any idea?

4 Answers 4


You can do this by using hook_form_FORM_ID_alter() and adding an extra validation function to the user register form:

function MYMODULE_form_user_register_form_alter(&$form, &$form_state, $form_id) {
  $form['#validate'][] = 'MYMODULE_validate_register_form';

function MYMODULE_validate_register_form(&$form, &$form_state) {
  if ($form_state['values']['mail'] != $form_state['values']['name']) {
    form_set_error('name', t('Name and email must be the same.'));

I'm not 100% positive those $form_state['values'] keys are the right ones so you'll need to inspect the form to make sure; but the logic is the same either way.


Maybe the Email registration module is something for you: http://drupal.org/project/email_registration

Allow users to register and login using only an email address. Users can then log-in using their email address and password for authentication.

  • I looked into that module but i think it could create some mess with the site. Since it is already up & running i don't think it's a good idea.
    – flero
    Commented Jun 27, 2012 at 7:26
  • The module just put's the given email address into the user name field. So if this is your current policy, this shouldn't be a problem. Of course you should test this kind of changes on a dev environment before putting it into production.
    – BetaRide
    Commented Jun 27, 2012 at 7:56
  • There is a half mess in that site and i don't want to create a bigger one... using a hook seems to be the cleanest solution.
    – flero
    Commented Jun 27, 2012 at 10:09

Clive's answer above is great, but if you prefer to work in the theme layer you can put this code into your theme's template.php file.

function MYTHEME_form_alter(&$form, &$form_state, $form_id){
  if ($form_id == 'user_register_form'){
    $form['#validate'][] = 'MYTHEME_validate_register_form';

function MYTHEME_validate_register_form(&$form, &$form_state) {
  if ($form_state['values']['mail'] != $form_state['values']['name']) {
    form_set_error('name', t('Name and email must be the same.'));

You can obviously change the validation statement to do whatever you want, you just have to make sure to pass "form_set_error()" to invalidate the form and display a message to the end user.


You can also use the Login Toboggon module to do all kinds of neat login-y things.

The LoginToboggan module offers several modifications of the Drupal login system in an external module by offering the following features and usability improvements:

  • Allow users to login using either their username OR their e-mail address.
  • Allow users to login immediately.
  • Provide a login form on Access Denied pages for non-logged-in (anonymous) users.
  • The module provides two login block options: One uses JavaScript to display the form within the block immediately upon clicking "log in". The other brings the user to a separate page, but returns the user to their original page upon login.
  • Customize the registration form with two e-mail fields to ensure accuracy.
  • Optionally redirect the user to a specific page when using the 'immediate login' feature.
  • Optionally redirect the user to a specific page upon validation of their e-mail address.
  • Optionally display a user message indicating a successful login.
  • Optionally combine both the login and registration form on one page.
  • Optionally have unvalidated users purged from the system at a pre-defined interval (please read the CAVEATS section of INSTALL.txt for important information on configuring this feature!).
  • Integrates with Rules module to do various tasks when a user validates via email validation process (see http://drupal.org/node/880904 for an example)

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.