【问题标题】:HTML5 - Password RegexHTML5 - 密码正则表达式
【发布时间】:2017-02-23 14:54:51
【问题描述】:

我尝试为密码创建一个有效的 html5 模式。长度至少为 9 个字符,并且至少包含此列表的一个大写字母、一个小写字母、一个数字和一个特殊字符

()[]{}?!$%&/=*+~,.;:-_

我做了这个正则表达式,但它不起作用...任何人都可以解决这个问题?

pattern="^(?=.*\d)(?=.*[a-z])(?=.*[()[]{}?!$%&/=*+~,.;:<>-_])(?=.*[A-Z]).{9,}?$"

【问题讨论】:

    标签: regex html passwords


    【解决方案1】:
    pattern="(?=.*\d)(?=.*[a-z])(?=.*[()\[\]{}?!$%&/=*+~,.;:<>_-])(?=.*[A-Z]).{9,}"
    

    有几个错误:

    ^(?=.*\d)(?=.*[a-z])(?=.*[()[]{}?!$%&/=*+~,.;:<>-_])(?=.*[A-Z]).{9,}?$
    # ] needs to be escaped ----^^                  ^                   ^
    # otherwise it will close the character class   |                   |
    # [ too but for no logical reason               |                   |
    # the - is used to define a character range ----+                   |
    # the range >-_ gives >?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_            |
    # there's no reason to make this quantifier non-greedy -------------+
    

    另外,锚点^$是隐式的,不用放。

    请注意,使用范围,您也可以这样编写模式:

    pattern="(?=.*\d)(?=.*[a-z])(?=.*[!$%&(-/:-?_{}~])(?=.*[A-Z]).{9,}"
    

    【讨论】:

      猜你喜欢
      • 2013-06-03
      • 1970-01-01
      • 1970-01-01
      • 2012-07-25
      • 2023-03-06
      • 2010-11-18
      相关资源
      最近更新 更多