1
def check_password(pw):
    global reason
    while True:
        if not re.search(string.ascii_lowercase,pw):
            flag = False
            reason = get_error(4)
            break

        elif not re.search(string.ascii_uppercase, pw):
            flag = False
            reason = get_error(1)
            break

        elif not re.search(string.digits, pw):            
            flag = False
            reason = get_error(2)
            break
        elif not re.search(string.punctuation, pw):          
            flag = False
            reason = get_error(3)
            break
        else:
            flag = True
            print("Valid Password") 
            break
        return flag

flag = False
min_length = 5

#password = input("Please enter your password:")
password = getpass.getpass("Enter Password:")

if len(password) >= min_length:
    check_password(password)
    print(password)
    if flag == True:
        print("Valid Password!")
    else:
        print(reason)
else:           
    print("Your password is not long enough.")

Here I am using regex to validate a password. I was successful if I hardcode the pattern "[A-Z]", "[0-9]", but now if I use string library constants, I am getting the message from the 1st check, irrespective of anything else wrong in the password.

Can you tell me where I am wrong, syntactically or semantically?

  • With `[a-z]` you check if any of the characters from `a` to `z` is in the string. With `string.ascii_lowercase` you check if `'abcdefghijklmnoprstuvwxyz'` is in the string. You might want to use `'[' + string.ascii_lowercase + ']'` (or just keep `[a-z]`). – Matthias Dec 02 '19 at 14:07
  • `return flag` is unreachable, `check_password` is returning `None`. You don't actually need loop there, remove it. – Guy Dec 02 '19 at 14:08
  • Thanks @Matthias. But how can I check for special Characters? I dont wanna hard code them – Jayit Ghosh Dec 02 '19 at 14:26
  • thanks for the loop edit @Guy! :-) – Jayit Ghosh Dec 02 '19 at 14:26
  • See [Reference - Password Validation](https://stackoverflow.com/q/48345922/3600709) – ctwheels Dec 03 '19 at 16:50

1 Answers1

0

The constants only contain chunks of characters, but you want to create character classes out of them. That means you need to enclose all of them with [ and ]. Also, you need to make sure some characters are escaped (^, -, ] and \), otherwise the punctuation regex will not be matching what you expect.

So, you need to replace string.ascii_lowercase with f"[{string.ascii_lowercase}]", string.ascii_uppercase with f"[{string.ascii_uppercase}]", string.digits with f"[{string.digits}]", string.punctuation with f"[{string.punctuation.replace("\\", "\\\\").replace("-", r"\-").replace("^", r"\^").replace("]", r"\]")}]".

If your Python environment is older than 3.7 and does not support string interpolation, use str.format, e.g. if not re.search(r"[{}]".format(string.ascii_lowercase),pw) and so on.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • I was afraid to use [ ] , as I thought that would probably turn it into a list, and i was getting a lot of errors during the function call. – Jayit Ghosh Dec 03 '19 at 03:55
  • @Jay with the guidance from me you can now use `[...]` without fear. There are no lists here, just character sets. Special chars could give you trouble but now you know which of them need escaping. – Wiktor Stribiżew Dec 03 '19 at 06:35