Index: wp-login.php
===================================================================
--- wp-login.php	(revision 21789)
+++ wp-login.php	(working copy)
@@ -297,40 +297,19 @@
  * @return int|WP_Error Either user's ID or error on failure.
  */
 function register_new_user( $user_login, $user_email ) {
-	$errors = new WP_Error();
+	$data = check_user_for_errors( $user_login, $user_email );	
+	// returns $errors, $user_login, $user_email
+	extract( $data );
 
-	$sanitized_user_login = sanitize_user( $user_login );
-	$user_email = apply_filters( 'user_registration_email', $user_email );
+	do_action( 'register_post', $user_login, $user_email, $errors );
 
-	// Check the username
-	if ( $sanitized_user_login == '' ) {
-		$errors->add( 'empty_username', __( '<strong>ERROR</strong>: Please enter a username.' ) );
-	} elseif ( ! validate_username( $user_login ) ) {
-		$errors->add( 'invalid_username', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ) );
-		$sanitized_user_login = '';
-	} elseif ( username_exists( $sanitized_user_login ) ) {
-		$errors->add( 'username_exists', __( '<strong>ERROR</strong>: This username is already registered, please choose another one.' ) );
-	}
+	$errors = apply_filters( 'registration_errors', $errors, $user_login, $user_email );
 
-	// Check the e-mail address
-	if ( $user_email == '' ) {
-		$errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please type your e-mail address.' ) );
-	} elseif ( ! is_email( $user_email ) ) {
-		$errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn&#8217;t correct.' ) );
-		$user_email = '';
-	} elseif ( email_exists( $user_email ) ) {
-		$errors->add( 'email_exists', __( '<strong>ERROR</strong>: This email is already registered, please choose another one.' ) );
-	}
-
-	do_action( 'register_post', $sanitized_user_login, $user_email, $errors );
-
-	$errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email );
-
 	if ( $errors->get_error_code() )
 		return $errors;
 
 	$user_pass = wp_generate_password( 12, false);
-	$user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email );
+	$user_id = wp_create_user( $user_login, $user_pass, $user_email );
 	if ( ! $user_id ) {
 		$errors->add( 'registerfail', sprintf( __( '<strong>ERROR</strong>: Couldn&#8217;t register you... please contact the <a href="mailto:%s">webmaster</a> !' ), get_option( 'admin_email' ) ) );
 		return $errors;
@@ -734,4 +713,4 @@
 <?php
 login_footer();
 break;
-} // end action switch
+} // end action switch
\ No newline at end of file
Index: wp-includes/user.php
===================================================================
--- wp-includes/user.php	(revision 21789)
+++ wp-includes/user.php	(working copy)
@@ -1264,18 +1264,12 @@
 		$user_pass = wp_hash_password($user_pass);
 	}
 
-	$user_login = sanitize_user($user_login, true);
-	$user_login = apply_filters('pre_user_login', $user_login);
+	$data = check_user_for_errors( $user_login, $user_email, $update );	
+	extract( $data );
 
-	//Remove any non-printable chars from the login string to see if we have ended up with an empty username
-	$user_login = trim($user_login);
-
-	if ( empty($user_login) )
-		return new WP_Error('empty_user_login', __('Cannot create a user with an empty login name.') );
-
-	if ( !$update && username_exists( $user_login ) )
-		return new WP_Error('existing_user_login', __('This username is already registered.') );
-
+	if ( $errors->get_error_code() )
+		return $errors;	
+	
 	if ( empty($user_nicename) )
 		$user_nicename = sanitize_title( $user_login );
 	$user_nicename = apply_filters('pre_user_nicename', $user_nicename);
@@ -1284,13 +1278,6 @@
 		$user_url = '';
 	$user_url = apply_filters('pre_user_url', $user_url);
 
-	if ( empty($user_email) )
-		$user_email = '';
-	$user_email = apply_filters('pre_user_email', $user_email);
-
-	if ( !$update && ! defined( 'WP_IMPORTING' ) && email_exists($user_email) )
-		return new WP_Error('existing_user_email', __('This email address is already registered.') );
-
 	if ( empty($display_name) )
 		$display_name = $user_login;
 	$display_name = apply_filters('pre_user_display_name', $display_name);
@@ -1494,3 +1481,45 @@
 	);
 	return apply_filters( 'user_contactmethods', $user_contactmethods, $user );
 }
+
+/**
+ * Combines error_checking code from wp_insert_user and register_new_user
+ * 
+ * @since 3.5.0
+ * 
+ * @param string $user_login The user's login 
+ * @param string $user_email The user's email address
+ * @param bool $update Whether the data is part of an update for an existing user
+ * @return array Data is meant to have extract() called on it
+ */
+function check_user_for_errors( $user_login, $user_email, $update = false ) {
+	$errors = new WP_Error();
+	
+	$user_login = apply_filters('pre_user_login', sanitize_user( $user_login, true ) );
+	$user_email = apply_filters( 'user_registration_email', $user_email );
+
+	$sanitized_user_login = trim( $user_login );	
+	
+	// Check the username
+	if ( empty( $sanitized_user_login ) ) {
+		$errors->add( 'empty_username', __( '<strong>ERROR</strong>: Please enter a username.' ) );
+	} elseif ( ! validate_username( $user_login ) ) {
+		$errors->add( 'invalid_username', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ) );
+		$sanitized_user_login = '';
+	} elseif ( ! $update && username_exists( $sanitized_user_login ) ) {
+		$errors->add( 'username_exists', __( '<strong>ERROR</strong>: This username is already registered, please choose another one.' ) );
+	}	
+	
+	$sanitized_user_email = apply_filters( 'pre_user_email', $user_email );
+	// Check the e-mail address
+	if ( empty( $sanitized_user_email ) ) {
+		$errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please type your e-mail address.' ) );
+	} elseif ( ! is_email( $sanitized_user_email ) ) {
+		$errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn&#8217;t correct.' ) );
+		$user_email = '';
+	} elseif ( ! $update && ! defined( 'WP_IMPORTING' ) && email_exists( $sanitized_user_email ) ) {
+		$errors->add( 'email_exists', __( '<strong>ERROR</strong>: This email is already registered, please choose another one.' ) );
+	}
+	
+	return array( 'errors' => $errors, 'user_email' => $sanitized_user_email, 'user_login' => $sanitized_user_login );
+}
