Regex to validate password strength

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

My password strength criteria is as below :

  • 8 characters length
  • 2 letters in Upper Case
  • 1 Special Character (!@#$&*)
  • <
11条回答
  •  眼角桃花
    2020-11-22 02:57

    Another solution:

    import re
    
    passwordRegex = re.compile(r'''(
        ^(?=.*[A-Z].*[A-Z])                # at least two capital letters
        (?=.*[!@#$&*])                     # at least one of these special c-er
        (?=.*[0-9].*[0-9])                 # at least two numeric digits
        (?=.*[a-z].*[a-z].*[a-z])          # at least three lower case letters
        .{8,}                              # at least 8 total digits
        $
        )''', re.VERBOSE)
    
    def userInputPasswordCheck():
        print('Enter a potential password:')
        while True:
            m = input()
            mo = passwordRegex.search(m) 
            if (not mo):
               print('''
    Your password should have at least one special charachter,
    two digits, two uppercase and three lowercase charachter. Length: 8+ ch-ers.
    
    Enter another password:''')          
            else:
               print('Password is strong')
               return
    userInputPasswordCheck()
    

提交回复
热议问题