简体   繁体   中英

How To check the validity of password according to input by users

To check the validity of password input by users.

Following are the criteria for checking the password:

  1. At least 1 letter between az.
  2. At least 1 number between 0-9
  3. At least 2 letter between AZ
  4. At least 2 character from $#@,. ETC
  5. Minimum length of transaction password: 6
  6. Maximum length of transaction password: 12

Answers to this question couldn't clear the problems

I tried this but it doesn't worked

N = [1,2,3,4,5,6,7,8,9,0]
A = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
S = ['!','@','#','$','%','~','`','^','&','*','(',')','_','+','=','-']

pasw = input('Password: ')
if any((word in pasw for word in N,A,S)):
  print ('OK')
else:
   print ('TRY LATER')

The best way is with a regex as suggested, but that is a whole new world if you don't know what one is. I suggest you read up.

But using code you understand it can be done:

pasw='PAssword1!!'

S = ['!','@','#','$','%','~','`','^','&','*','(',')','_','+','=','-']
upper,lower,number,special = 0,0,0,0

for n in pasw:
    if n.islower():
        lower=1
    if n.isnumeric():
        number=1
    if n.isupper():
        upper+=1
    if n in S:
        special+=1

if len(pasw) >= 6 and len(pasw) <= 12 and lower > 0 and number > 0 and special > 1 and upper > 1:
    print('OK')
else:
    print('TRY LATER')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM