Change the e-mail value during user registration ?  form_set_value() I have a custom module (entitled ta_module) : in the user registration form, it hides the e-mail field, and add a custom field where user have to enter what's before @ and then choose his e-mail domain name between the one proposed in a list next to it.

Here's the code :

&lt;?php

/**
 * Implementation of hook_form_alter()
 */
function ta_module_form_alter(&amp;$form, &amp;$form_state, $form_id) {

   // If the form is the user_register form
    if ($form_id == 'user_register_form') {
        // Attach our custom validation function as the first validation function
        array_unshift($form['#validate'], '_ta_module_user_validate');
        // Hide the email address field
        $form['account']['mail']['#type'] = 'hidden';
        // Give the email address field a random default value (in this case a random number and a timestamp)
        // as we cannot unset it due to Drupal's validation rules
        $form['account']['mail']['#default_value'] = rand(1111, 9999) . '@' .time() . '.com';
        // Add our new text entry field
        $form['mail_first'] = array(
      '#type' =&gt; 'textfield',
      '#title' =&gt; t('Email address'),
      '#default_value' =&gt; '',
      '#size' =&gt; 35,
      '#maxlength' =&gt; 64,
      '#required' =&gt; TRUE,
    );
        // Add our select list of possible email domain values
        $form['mail_last'] = array(
      '#type' =&gt; 'select',
      '#title' =&gt; 'Domaine',
      '#title_display' =&gt; 'before',
      '#options' =&gt; array(
              // These are the possible email address domains
              'myentreprise.net' =&gt; '@myentreprise.net',
              'myentreprise.com' =&gt; '@myentreprise.com',
            ),
      '#default_value' =&gt; 'myentreprise.net',
      '#required' =&gt; TRUE,
    );
    }
}

/**
 * Function to piece together our email address
 */
function _ta_module_user_validate(&amp;$form, &amp;$form_state) {
    // If our two form elements have values
    if (!empty($form_state['values']['mail_first']) &amp;&amp; !empty($form_state['values']['mail_last'])) {
        // Set the email address to the correct value
        $form_state['complete form']['account']['mail']['#value'] = $form_state['input']['mail_first'] . '@' . $form_state['input']['mail_last'];
    }
}


(see This Q/A for more infos).

All appear quite brightly, but when the user click on "create a new account", the user is created but the last function of my module doesn't work : the e-mail finally saved is the default value (something like 0129A@I1I101IZ10IZ.com) instead of the two pieces joined together...

HOW TO FIX THIS : Thus how to assign to mail the value of mail-first + mail-last ?

/!\ I'm working on Drupal 7, so I think it's an issue of uncorrect variables...

Thks for reading !  ;)
If you need to set the value for the form field used to contain the email address, then you should use code similar to the following one:

function _ta_module_user_validate(&amp;$form, &amp;$form_state) {
  if (!empty($form_state['values']['mail_first']) &amp;&amp; !empty($form_state['values']['mail_last'])) {
    form_set_value($form['account']['mail'], $form_state['values']['mail_first'] . '@' . $form_state['values']['mail_last'], $form_state);
  }
}


As an example of how form_set_value() is used, see system_site_information_settings_validate(), which contains the following code:

// Check for empty front page path.
if (empty($form_state['values']['site_frontpage'])) {
  // Set to default "node".
  form_set_value($form['front_page']['site_frontpage'], 'node', $form_state);
}
else {
  // Get the normal path of the front page.
  form_set_value($form['front_page']['site_frontpage'], drupal_get_normal_path($form_state['values']['site_frontpage']), $form_state);
}