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.
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 );
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.
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 );
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
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.