Python @ DjangoSpin

PyPro #29 Password Validator

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 1 minutes

Password Validator in Python: Build a validatePassword() function which accepts a password as string and reports if it is a good password or not based on the following constraints:

Password Validator in Python

  • must contain at least 1 special character
  • must contain at least 1 numeric character
  • must contain at least 1 uppercase character
  • must contain at least 1 lowercase character
  • must be at least 8 characters in length but not more than 12 characters
def validatePassword(enteredPassword):
    containsSpecialCharacter = False
    containsNumericCharacter = False
    containsUppercaseCharacter = False
    containsLowercaseCharacter = False

    # Setting above flags to True if conditions are met
    for character in enteredPassword:
        if character in '!@#$%^&*()_+':
            containsSpecialCharacter = True
        elif character in '1234567890':
            containsNumericCharacter = True
        elif character in 'QWERTYUIOPASDFGHJKLZXCVBNM':
            containsUppercaseCharacter = True
        elif character in 'qwertyuiopasdfghjklzxcvbnm':
            containsLowercaseCharacter = True

    # Checking if all constraints are met 
    if (len(enteredPassword) >= 8 and len(enteredPassword) <= 12 
       and containsSpecialCharacter and containsNumericCharacter 
       and containsUppercaseCharacter and containsLowercaseCharacter):
           print(enteredPassword, "is a good password.")
    else:
        print(enteredPassword, "is NOT a good password.")

enteredPassword = input("Please enter the password to be validated: ")
validatePassword(enteredPassword)
Try it here.

See also:


Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon