0

如何限制密码,使其不允许第一个或最后一个字符为非字母字符(参见“标题”)?

我的 HTML 代码:

<form action = "." method="post">
  <input type="password" 
    placeholder = "New Password..." 
    name= "password" 
    title = "Password must be at least 8 characters, at least one alphabetic character [a-zA-Z], at least one non-alphabetic character, which is not the first or last character." 
    pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}">
</form>

*hi123HI示例:密码 hi123*HI 被接受,但由于第一个字符是第一个字符,我如何拒绝*(如果*是最后一个字符,或者特殊字符或数字既是第一个字符又是最后一个字符)。

4

1 回答 1

0

添加负前瞻以检查初始字符和最终字符:

/(?![^A-Z])(?!.*[^A-Z]$)(?=.*[A-Z])(?=.*[^A-Z]).{8,}/i
 ^^^^^^^^^^                                               NO INITIAL SPECIAL CHAR
           ^^^^^^^^^^^^^                                  NO FINAL SPECIAL CHAR
                        ^^^^^^^^^^^                       AT LEAST ONE LETTER
                                   ^^^^^^^^^^^^           AT LEAST ONE SPECIAL CHAR
                                               ^^^^^^     EIGHT OR MORE CHARS
于 2017-06-21T05:11:48.010 回答