|
| 1 | +import string |
| 2 | + |
| 3 | +def passwordValidator(): |
| 4 | + """ |
| 5 | + Validates passwords to match specific rules |
| 6 | + : return: str |
| 7 | + """ |
| 8 | + # display rules that a password must conform to |
| 9 | + print('\nYour password should: ') |
| 10 | + print('\t- Have a minimum length of 6;') |
| 11 | + print('\t- Have a maximum length of 12;') |
| 12 | + print('\t- Contain at least one uppercase letter;') |
| 13 | + print('\t- Contain at least one lowercase letter;') |
| 14 | + print('\t- Contain at least a number;') |
| 15 | + print('\t- Contain at least a special character (such as @,+,£,$,%,*^,etc);') |
| 16 | + print('\t- Not contain space(s).') |
| 17 | + |
| 18 | + # get user's password |
| 19 | + userPassword = input('\nEnter a valid password: ').strip() |
| 20 | + |
| 21 | + # check if user's password conforms to the rules above |
| 22 | + if not (6 <= len(userPassword) <= 12): |
| 23 | + message = 'Invalid Password..your password should have a minimum ' |
| 24 | + message += 'length of 6 and a maximum length of 12' |
| 25 | + return message |
| 26 | + |
| 27 | + if ' ' in userPassword: |
| 28 | + message = 'Invalid Password..your password shouldn\'t contain space(s)' |
| 29 | + return message |
| 30 | + |
| 31 | + if not any(i.isupper() for i in userPassword): |
| 32 | + message = 'Invalid Password..your password should contain at least one uppercase letter' |
| 33 | + return message |
| 34 | + |
| 35 | + if not any(i.islower() for i in userPassword): |
| 36 | + message = 'Invalid Password..your password should contain at least one lowercase letter' |
| 37 | + return message |
| 38 | + |
| 39 | + if not any(i in string.digits for i in userPassword): |
| 40 | + message = 'Invalid Password..your password should contain at least a number' |
| 41 | + return message |
| 42 | + |
| 43 | + if not any(i in string.punctuation for i in userPassword): |
| 44 | + message = 'Invalid Password..your password should contain at least a special character' |
| 45 | + return message |
| 46 | + |
| 47 | + return 'Valid Password!' |
| 48 | + |
| 49 | + |
| 50 | +if __name__ == "__main__": |
| 51 | + my_password = passwordValidator() |
| 52 | + print(my_password) |
0 commit comments