diff --git a/PASSWORD RELATED/password-validator/PASSWORD_VALIDATOR.py b/PASSWORD RELATED/password-validator/PASSWORD_VALIDATOR.py index 76dfbef5..a24719a6 100644 --- a/PASSWORD RELATED/password-validator/PASSWORD_VALIDATOR.py +++ b/PASSWORD RELATED/password-validator/PASSWORD_VALIDATOR.py @@ -9,7 +9,8 @@ def passwordValidator(): print('\nYour password should: ') print('\t- Have a minimum length of 6;') print('\t- Have a maximum length of 12;') - print('\t- Contain at least an uppercase letter or a lowercase letter') + print('\t- Should not start with a number;') + print('\t- Contain at least one uppercase letter and one lowercase letter;') print('\t- Contain at least a number;') print('\t- Contain at least a special character (such as @,+,£,$,%,*^,etc);') print('\t- Not contain space(s).') @@ -17,25 +18,40 @@ def passwordValidator(): userPassword = input('\nEnter a valid password: ').strip() # check if user's password conforms # to the rules above + + # Check length of password (6 to 12 characters) if not(6 <= len(userPassword) <= 12): - message = 'Invalid Password..your password should have a minimum ' + message = 'Invalid Password: Password should have a minimum ' message += 'length of 6 and a maximum length of 12' return message + + # Check if first character is a digit + if userPassword and not(userPassword[0].isalpha()): + message = "Invalid Password: First character should be a letter." + return message + + # Check for spaces in password (Password mustn't contain any spaces) if ' ' in userPassword: - message = 'Invalid Password..your password shouldn\'t contain space(s)' - return message - if not any(i in string.ascii_letters for i in userPassword): - message = 'Invalid Password..your password should contain at least ' - message += 'an uppercase letter and a lowercase letter' + message = 'Invalid Password: Password shouldn\'t contain space(s)' return message - if not any(i in string.digits for i in userPassword): - message = 'Invalid Password..your password should contain at least a number' + + # Check for at least 1 upper case and 1 lower case letter in password + if not (any(c.islower() for c in userPassword) and any(c.isupper() for c in userPassword)): + message = "Invalid Password: Password should contain both uppercase and lowercase letters." + return message + + # Check for at least 1 digit in password + if not any(c.isdigit() for c in userPassword): + message = 'Invalid Password: Password should contain at least one number.' return message + + # Check for at least 1 special character in password if not any(i in string.punctuation for i in userPassword): - message = 'Invalid Password..your password should contain at least a special character' + message = 'Invalid Password: Password should contain at least a special character' return message - else: - return 'Valid Password!' + + # If no condition fails - password considered to be valid + return 'Valid Password!' my_password = passwordValidator() print(my_password) \ No newline at end of file diff --git a/PASSWORD RELATED/password-validator/README.md b/PASSWORD RELATED/password-validator/README.md index 8ce2d6f7..21d7b3a4 100644 --- a/PASSWORD RELATED/password-validator/README.md +++ b/PASSWORD RELATED/password-validator/README.md @@ -3,10 +3,11 @@ This program validates passwords to match specific rules. A valid password is one that conforms to the following rules: - Minimum length is 6; - Maximum length is 12; -- Contains at least an uppercase letter or a lowercase letter -- Contains at least a number; -- Contains at least a special character (such as @,+,£,$,%,*^,etc); -- Doesn't contain space(s). +- Must not start with a number +- Contains at least one uppercase letter and one lowercase letter +- Contains at least one number; +- Contains at least one special character (such as @,+,£,$,%,*^,etc); +- Does not contain space(s). # Prerequisites