Thursday, 26 April 2018

python - Password validity checker using python

Password validity checker using python


  • You need to implement a password validity checker.  A password is valid if all the given rules are satisfied, else it is invalid.
    •  At least 1 (one) lower case letter ([a-z]) is present
    •  At least 1 numeric character ([0-9]) is present  
    •  At least 1 (one) upper  case letter ([A-Z]) is present
    •  At least 1 character from [$#@] is present
    •  Minimum and Maximum length allowed must be 6 and 12 respectively
  • Input : You need to read a string from STDIN and check if it is a valid or invalid password.
  • Output : The string valid or invalid printed.

  • Test Cases:

    • Input: ABCDa$$$$$56
    • Output: valid

    • Input: 12345abcd111
    • Output: invalid
    • Input: 2w3E*
    • Output: invalid

Program:

def main():
    password = raw_input("Enter value :")

    digit = 0
    upper = 0
    lower = 0
    special = 0
    flag_len = 0

    for char in password:
        if char.isdigit():
            digit = 1
        if char.isupper():
            upper = 1
        if char.islower():
            lower = 1
        if char == '$' or  char == '#' or char == '@':
            special = 1

    length = len(password)
    if length >= 6 and length <= 12:
        flag_len = 1

    if digit and upper and lower and special and flag_len:
       print "valid"
    else:
       print "invalid"

main()


Output:

python$ python passwordVali.py
Enter value :Velraj
invalid
python$
python$
python$ python passwordVali.py
Enter value :Velraj12#
valid

No comments:

Post a Comment