jeudi 8 juillet 2021

How to detect a special character in password-string in python

Im making a program where you can add password to the database. The password should contain one uppercase, one lowercase, one digit, one special character and should be between 8 and 30 characters. The rest works except for the special character part. I put a special character in the string but my program still tells me it has no special character. Im not sure what im doing wrong here:

specialCharacters = "~!@#$%^&*_-+=`|\(){}[]:;'<>,.?/"

def checkPassword(password):
    global specialCharacters
    errorString = ""
    if(len(password) < 8 or len(password) > 30):
        errorString = "ERROR: Your password must be between 8 and 30 characters"
        print(errorString)
        return False
    elif(any(char.isalpha() for char in password) == False):
        errorString = ("ERROR: password must contain at least letter")
        print(errorString)
        return False
    elif(any(char.isupper() for char in password) == False):
        errorString = ("ERROR: password must contain at least one uppercase")
        print(errorString)
        return False
    elif(any(char.islower() for char in password) == False):
        errorString = ("ERROR: password must contain at least one lowercase")
        print(errorString)
        return False
    elif(any(char.isdigit() for char in password) == False):
        errorString = ("ERROR: password must contain at least one digit")
        print(errorString)
        return False
    for ch in password:                   /// here is the problem
        if ch not in specialCharacters:
            errorString = "ERROR: password must contain at least one special character"
            print(errorString)
            return False
    else:
        return True


checkPassword("Hellooooo1*") // returns "ERROR: password must contain at least one special character", why? 



Aucun commentaire:

Enregistrer un commentaire