【发布时间】:2019-04-29 15:27:18
【问题描述】:
我网站上的用户名只能包含 A-Z、a-z、0-9。没有空格或特殊字符。
我的注册表单中有以下 php 代码,但是当我输入仅包含字母的用户名时,它会输出错误,有什么帮助吗?:
if (!preg_match('/[^a-zA-Z0-9]+/gm', $_POST["userName"])) {
$output = json_encode(
array(
'type' => 'error',
'text' => 'Your username must not contain any spaces or special characters.'
));
die($output);
}
【问题讨论】:
-
您究竟为什么要这样做?
-
regex101.com/r/W67Jf9/1,
^应该在列表之外 -
没有
g修饰符。 -
使用
'/^[a-zA-Z0-9]+$/'