Password validation in Python without Regular Expression
In this tutorial, we take a password as input and check whether the given password is valid or not under certain conditions without using the RegEx module in Python language.
Password is said to be strong and valid if it satisfies the given conditions, ie, minimum strength, a combination of number, letter, special character etc. It is important to keep strong passwords for the users to ensure their data security.
Conditions required for a valid password
Password:
- Password strength should be between 6 to 20 characters long
- should contain at least one uppercase and one lowercase character.
- must have at least one number.
- should have at least one special symbol.
Program code for password validation using Naive method
Function to validate the password
def password_validate(password): SpecialSymbol =['$', '@', '#', '%'] val = True if len(password) < 6: print('length should be at least 6') val = False if len(password) > 20: print('length should be not be greater than 8') val = False if not any(char.isdigit() for char in password): print('Password should have at least one numeral') val = False if not any(char.isupper() for char in password): print('Password should have at least one uppercase letter') val = False if not any(char.islower() for char in password): print('Password should have at least one lowercase letter') val = False if not any(char in SpecialSymbol for char in password): print('Password should have at least one of the symbols [email protected]#') val = False if val: return val
The main method of the program
def main(): password = input("Enter the Password: ") if (password_validate(password)): print("Password is valid") else: print("Password is invalid!!")
The final and last driver code of the program
if __name__ == '__main__': main()
After combining these three parts of the program, we will be getting the result as the given password is valid or invalid. In this program, the user gets the opportunity to enter a password and check whether his/her password is valid or invalid. Here, the first output when I entered [email protected] as my password is
Enter the Password: [email protected] Password should have at least one numeral Invalid Password !!
and then when I entered [email protected] as my password, here is the output.
Enter the Password: [email protected] Password is valid
In this tutorial, I have shown an invalid password and a valid password as an example of the output according to my given conditions. The conditions are modifiable according to the programmer or company requirements. It is always safe to keep a strong password for better data security.
Leave a Reply