Title: Username Spaces
Last modified: March 29, 2026

---

# Username Spaces

 *  Resolved [codings](https://wordpress.org/support/users/codings/)
 * (@codings)
 * [6 days, 1 hour ago](https://wordpress.org/support/topic/username-spaces/)
 * Hi UM Team, thank you for this plugin!
 * Is there a way to prevent new users from registering a username with spaces? 
   With an error message telling them to use “username” and not “user name”?
 * I couldn’t find a native option in Ultimate Member settings, so I tried this 
   function:
 *     ```wp-block-code
       add_filter( 'validate_username' , 'custom_validate_username', 10, 2);add_filter('validate_username', function (bool $isValid, string $username): bool {    if (preg_match('/[^a-z]/', $username)) {        return false;    }        return $isValid;});
       ```
   
 * Also tried this to prevent capital letters:
 *     ```wp-block-code
       add_filter( 'validate_username' , 'custom_validate_username', 10, 2);function custom_validate_username( $valid, $username ) {    if ( preg_match("/\\s/", $username) ) {        // There are spaces, return a WP_Error object with a custom message        return new WP_Error( 'username_has_spaces', __( '<strong>Error</strong>: Usernames cannot contain spaces.', 'your-text-domain' ) );    }    return $valid;}
       ```
   
 * Neither functions are working and I think it may be because UM is handling the
   registration. It seems like a good idea to have some control over what is allowed
   when a member creates a new username. Is there a way to achieve this?

Viewing 1 replies (of 1 total)

 *  Thread Starter [codings](https://wordpress.org/support/users/codings/)
 * (@codings)
 * [5 days, 22 hours ago](https://wordpress.org/support/topic/username-spaces/#post-18866327)
 * Anyone wanting to prevent use of spaces in usernames, the above code is incorrect.
 * [The correct answer is here](https://wordpress.org/support/topic/re-restrict-usernames-to-not-include-spaces/#post-16837796),
   with code below placed in the functions.php (I still think this would be a good
   option to put in the settings).
 *     ```wp-block-code
       add_action( 'um_submit_form_register', 'um_submit_form_register_no_spaces', 9, 1 );
   
       function um_submit_form_register_no_spaces( $args ) {
   
           if ( isset( $args['user_login']) && strpos( trim( $args['user_login'] ), ' ' ) > 0 ) {
               UM()->form()->add_error( 'user_login', __( 'You are not allowed to have spaces in your username.', 'ultimate-member' ) );
           }
       }
       ```
   
 * UM Team, is there a way to add to that function to also prevent use of capitalization
   in usernames?

Viewing 1 replies (of 1 total)

You must be [logged in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fusername-spaces%2F%3Foutput_format%3Dmd&locale=en_US)
to reply to this topic.

 * ![](https://ps.w.org/ultimate-member/assets/icon-256x256.png?rev=3160947)
 * [Ultimate Member – User Profile, Registration, Login, Member Directory, Content Restriction & Membership Plugin](https://wordpress.org/plugins/ultimate-member/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/ultimate-member/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/ultimate-member/)
 * [Active Topics](https://wordpress.org/support/plugin/ultimate-member/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/ultimate-member/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/ultimate-member/reviews/)

## Tags

 * [feature request](https://wordpress.org/support/topic-tag/feature-request/)
 * [usernames](https://wordpress.org/support/topic-tag/usernames/)

 * 1 reply
 * 1 participant
 * Last reply from: [codings](https://wordpress.org/support/users/codings/)
 * Last activity: [5 days, 22 hours ago](https://wordpress.org/support/topic/username-spaces/#post-18866327)
 * Status: resolved