简体   繁体   English

如何根据用户输入检查密码的有效性

[英]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. az之间至少1个字母。
  2. At least 1 number between 0-9 0-9之间至少1个数字
  3. At least 2 letter between AZ AZ之间至少2个字母
  4. At least 2 character from $#@,. $#@,中至少2个字符。 ETC 等等
  5. Minimum length of transaction password: 6 交易密码的最小长度:6
  6. Maximum length of transaction password: 12 交易密码的最大长度: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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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