Drupal 6: Adding validation to the user edit form to ensure username is a valid email address

At some point you might want add validation to the user edit form to ensure username is a valid email address. You can accomplish this by adding a form_alter hook, testing for the user profile form, and adding a validation handler to the $form object.

<?php
function MYMODULE_form_alter(&$form, $form_state, $form_id) {
  // test for the user profile edit form
  if ($form_id == 'user_profile_form') {
    // add another validation function to the list
    $form['#validate'][] = '_MYMODULE_form_alter_user_profile_validate';
  }
}

// define the validation function
function _MYMODULE_form_alter_user_profile_validate($form, &$form_state) {
  // validate the username and set a form error as necessary
  if (!valid_email_address($form_state['values']['name']))
    form_set_error('name', 'Username must be a valid email address.');
}

?>