Regex to validate password strength

后端 未结 11 747
天涯浪人
天涯浪人 2020-11-22 02:13

My password strength criteria is as below :

  • 8 characters length
  • 2 letters in Upper Case
  • 1 Special Character (!@#$&*)
  • <
11条回答
  •  -上瘾入骨i
    2020-11-22 03:04

    You should also consider changing some of your rules to:

    1. Add more special characters i.e. %, ^, (, ), -, _, +, and period. I'm adding all the special characters that you missed above the number signs in US keyboards. Escape the ones regex uses.
    2. Make the password 8 or more characters. Not just a static number 8.

    With the above improvements, and for more flexibility and readability, I would modify the regex to.

    ^(?=(.*[a-z]){3,})(?=(.*[A-Z]){2,})(?=(.*[0-9]){2,})(?=(.*[!@#$%^&*()\-__+.]){1,}).{8,}$
    

    Basic Explanation

    (?=(.*RULE){MIN_OCCURANCES,})     
    

    Each rule block is shown by (?=(){}). The rule and number of occurrences can then be easily specified and tested separately, before getting combined

    Detailed Explanation

    ^                               start anchor
    (?=(.*[a-z]){3,})               lowercase letters. {3,} indicates that you want 3 of this group
    (?=(.*[A-Z]){2,})               uppercase letters. {2,} indicates that you want 2 of this group
    (?=(.*[0-9]){2,})               numbers. {2,} indicates that you want 2 of this group
    (?=(.*[!@#$%^&*()\-__+.]){1,})  all the special characters in the [] fields. The ones used by regex are escaped by using the \ or the character itself. {1,} is redundant, but good practice, in case you change that to more than 1 in the future. Also keeps all the groups consistent
    {8,}                            indicates that you want 8 or more
    $                               end anchor
    

    And lastly, for testing purposes here is a robulink with the above regex

提交回复
热议问题