php - how can I disallow special characters, space, capital letter, dot in user name on registration?

admin2025-01-07  7

i am running membership site. any one can registered on my site.

i want to disallow special characters(ie:!@#$%^&*), space, capital letter, dot(.) in user name on registration.

i don't have any code that can cover all my (special characters, space, capital letter, dot) requirement. i am using separate separate function to stop this.

For Capital Latter Disallow In Usernames :

add_filter( 'sanitize_user', 'wpse_83689_lower_case_user_name' );
function wpse_83689_lower_case_user_name( $name ) {
if ( function_exists( 'mb_strtolower' ) )
    return mb_strtolower( $name );

return strtolower( $name );

For Spaces Disallow In Usernames :

add_filter('validate_username' , 'custom_validate_username', 10, 2);

function custom_validate_username($valid, $username ) {
    if (preg_match("/\\s/", $username)) {
        // there are spaces
        return $valid=false;
    }

return $valid;
}

i did lots of google but i did not find any solution for disallow Dot and Special Character.

can any one solve my this problem and combine all 4 option in single function?

sorry for my bad english.

thanks

i am running membership site. any one can registered on my site.

i want to disallow special characters(ie:!@#$%^&*), space, capital letter, dot(.) in user name on registration.

i don't have any code that can cover all my (special characters, space, capital letter, dot) requirement. i am using separate separate function to stop this.

For Capital Latter Disallow In Usernames :

add_filter( 'sanitize_user', 'wpse_83689_lower_case_user_name' );
function wpse_83689_lower_case_user_name( $name ) {
if ( function_exists( 'mb_strtolower' ) )
    return mb_strtolower( $name );

return strtolower( $name );

For Spaces Disallow In Usernames :

add_filter('validate_username' , 'custom_validate_username', 10, 2);

function custom_validate_username($valid, $username ) {
    if (preg_match("/\\s/", $username)) {
        // there are spaces
        return $valid=false;
    }

return $valid;
}

i did lots of google but i did not find any solution for disallow Dot and Special Character.

can any one solve my this problem and combine all 4 option in single function?

sorry for my bad english.

thanks

Share Improve this question asked Sep 29, 2017 at 9:36 gul rathodgul rathod 632 silver badges7 bronze badges 2
  • 1 Possible duplicate of Wordpress User Name Limitations – flomei Commented Sep 29, 2017 at 10:08
  • thanks but sorry to say i don't understand that function. – gul rathod Commented Sep 29, 2017 at 10:30
Add a comment  | 

2 Answers 2

Reset to default 0

please try below function for disallow special characters, space, capital letter, dot in user name on registration

 //add a filter to invalidate a username with spaces,special characters, capital letter, dot
    add_filter('validate_username','restrict_space_in_username',10,2);
    function restrict_space_in_username($valid,$user_name){
     //check if there is an space
     if ( preg_match('/\s[!@#$%^&*()+=\-\[\]\';,.\/{}|":<>?~\\\\][A-Z](.)/',$user_name) )
     return false;//if myes, then we say it is an error
     return $valid;//otherwise return the actual validity
    }

Use a regular expression that matches everything except lowercase ASCII characters. You can easily add more characters you deem allowable (maybe underscores _?) or even some regex magic:

add_filter('validate_username', function (bool $isValid, string $username): bool {
    if (preg_match('/[^a-z]/', $username)) {
        return false;
    }
    
    return $isValid;
});

The validate_user filter is checked inside register_new_user and prevents the user from registering if false is returned while showing the user an error message.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736259235a574.html

最新回复(0)