Is there a way to disallow spaces in the user name registration form in drupal 7?
I recently wrote a custom module to disallow spaces and other characters in usernames. Feel free to comment on the code. I also modified username_check to do the same validations in the ajax callback.

//implement hook_form_FORM_ID_alter
function custom_form_user_register_form_alter(&amp;$form, $form_state, $form_id) {
  //define validation function
  $form['#validate'][] = 'my_user_register_validate';
  //overwrite the username help text
  $form['account']['name']['#description'] = 'Username has to be between 5-15 characters long and can only contain letters and numbers';
}

function my_user_register_validate($form, &amp;$form_state) {

  $output['error'] = FALSE;
  $username = $form_state['input']['name'];
  $password = $form_state['input']['pass']['pass1'];

  if (strlen($username) &lt; 5 || strlen($username) &gt; 15) {
    $output['error'] = TRUE;
    $output['msg'] = t('The username has to be between 5 and 15 characters long. ');
  }
  if (strpos($username,' ') &gt; 0 || strpos($username,'.') &gt; 0 || strpos($username,'-') &gt; 0 || strpos($username,'_') &gt; 0 || strpos($username, "'") !== FALSE ){
    $output['error'] = TRUE;
    $output['msg'] = t('Username cannot contain spaces . - _ ' . chr(39)  );
  }

//display username errors
  if ($output['error']){
    //drupal_set_message( $output['msg'], 'error');
    form_set_error('name', $output['msg']);
  }
}