我正在运行会员网站。任何人都可以在我的网站上注册。
我不允许使用特殊字符(即:!@#$%^&;*)、空格、大写字母、点(.)注册时使用用户名。
我没有任何代码可以满足我的所有(特殊字符、空格、大写字母、dot)要求。我正在使用单独的函数来停止此操作。
对于用户名中不允许的大写字母:
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;
}
我做了很多谷歌,但我没有找到任何不允许点和特殊字符的解决方案。
有谁能解决我的这个问题,并将所有4个选项组合在一个函数中?
对不起,我的英语不好。
谢谢
SO网友:Shital Marakana
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
}