使用ctype_alnum()时,如何添加_
和-
作为有效字符的例外?
if ( ctype_alnum ($username) ) {
return TRUE;
} elseif (!ctype_alnum ($username)) {
$this->form_validation->set_message(
'_check_username',
'Invalid username! Alphanumerics only.'
);
return FALSE;
}
Run Code Online (Sandbox Code Playgroud)
Boo*_*eus 19
由于ctype_alnum仅验证字母数字字符,请尝试以下操作:
$sUser = 'my_username01';
$aValid = array('-', '_');
if(!ctype_alnum(str_replace($aValid, '', $sUser))) {
echo 'Your username is not properly formatted.';
}
Run Code Online (Sandbox Code Playgroud)
preg_match
也许可以使用吗?
if(preg_match("/^[a-zA-Z0-9_\-]+$/", $username)) {
return true;
} else {
$this->form_validation->set_message('_check_username', 'Invalid username! Alphanumerics only.');
}
Run Code Online (Sandbox Code Playgroud)