Regular Expression For Password Field

前端 未结 2 1949
耶瑟儿~
耶瑟儿~ 2021-01-23 01:04

I need a regular expression for a password field that:

  • Must have 1 number

  • Must have 1 letter (uppercase)

  • Must have 1 letter (low

相关标签:
2条回答
  • 2021-01-23 01:35

    You can change the base at the end:

    ((?=.*\d)(?=.*[a-z])(?=.*[A-Z])[^\W_]{8,})
    

    This solution expects your regex engine to be anchored. If not, anchor them with ^$.

    [^\W_] is negated character class. It asserts that this character is not a word character or _.

    As word characters covers alphanumeric characters and underscores, this double-negated character class shorthand [^\W_] is well-used for these scenarios.

    You can use [[:alnum:]] as well, if your regex engine supports ascii classes.

    Here is a regex demo!

    0 讨论(0)
  • 2021-01-23 01:38

    Use [a-zA-Z0-9] instead of . and anchor your regex:

    ^((?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{8,})$
    
    0 讨论(0)
提交回复
热议问题