我正在使用 jquery 插件 Bootstrap 验证器进行实时前端验证。
当我添加模式/正则表达式验证来验证密码(任何至少 8 个字符长的字符串)时,出现以下错误
Unable to check
<input pattern='^.*{8,}$'>
because the pattern is not a valid regexp: raw brace is not allowed in regular expression with unicode flag
HTML 是
<input pattern="^.*{8,}$" class="form-control" required="required" name="user[password]" id="user_password" type="password">
我应该如何编写正则表达式?
最佳答案
您应该改用 ^.{8,}$
。
此正则表达式对任何字符均有效 - 8 次或更多。
请注意,它还允许在密码中使用空格。如果你想允许任何不是空格的字符,你可以使用 ^[^\s]{8,}$
代替。
你当前的正则表达式实际上是
accept any char (
.
) - any number of times (*), and accept it (the "any char any number of times") with at least 8 chars{8,}
(which makes no sense).
关于javascript - 密码正则表达式 - 带有 unicode 标志的正则表达式中不允许原始大括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40660329/