我正在尝试使用 PHP 验证密码,我想对 Javascript 使用相同的正则表达式,在我的 PHP 中,当我尝试使用以下正则表达式时出现错误。 (Warning: filter_var(): Compilation failed: range out of order in character class at offset 14)
/^[a-zA-Z0-9_-\]\[\?\/<~#`!@\$%\^&\*\(\)\+=\}\|:\";\',>\{]{4,20}$/
if (!filter_var($password,FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z0-9_-\]\[\?\/<~#`!@\$%\^&\*\(\)\+=\}\|:\";\',>\{]{4,20}$/")))){
$errors .= "Please enter a valid password.";
}
我希望密码为0-9
, a-zA-Z
这些字符 ] [ ? / < ~ # ! @ $ % ^ & * ( ) + = } | : " ; ' , > { space
加`
我想在前端和后端使用正则表达式。
最佳答案
看看这个“模块化”正则表达式
/(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}/.test(password)
这个密码应该有:
-
(?=.*\d)
= 数字 -
(?=.*[a-z])
= 小写字母 -
(?=.*[A-Z])
=上限。字母 -
.{8,}
= 至少 8 个字符长
特殊字符部分: 取自上述答案之一,感谢@stribizhev
-
(?=.*[?\/<~#`!@$%^&*()+=}|:\";\',>{ -])
= 特殊字符
所以你最终得到:
/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[?\/<~#`!@$%^&*()+=}|:\";\',>{ -])/.test(password)
强制最小长度 .{n,} n = lenght
/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[?\/<~#`!@$%^&*()+=}|:\";\',>{ -]).{8,}/.test(pass)
关于javascript - 用于在 PHP 和 Javascript 中进行简单密码验证的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32724170/