Let’s take a password as a combination of alphanumeric characters along with special characters, and check whether the password is valid or not with the help of few conditions.
Conditions for a valid password are:
- Should have at least one number.
- Should have at least one uppercase and one lowercase character.
- Should have at least one special symbol.
- Should be between 6 to 20 characters long.
Input : Geek12# Output : Password is valid. Input : asd123 Output : Invalid Password !!
We can check if a given string is eligible to be a password or not using multiple ways.
Method #1: Naive Method (Without using Regex).
Password is valid
This code used boolean functions to check if all the conditions were satisfied or not. We see that though the complexity of the code is basic, the length is considerable.
Method #2: Using regex
compile() method of Regex module makes a Regex object, making it possible to execute regex functions onto the pat variable. Then we check if the pattern defined by pat is followed by the input string passwd. If so, the search method returns true, which would allow the password to be valid.
Password is valid.
- Name validation using IGNORECASE in Python Regex
- Python | Form validation using django
- Generating Strong Password using Python
- Python | Program to generate one-time password (OTP)
- Python | Random Password Generator using Tkinter
- getpass() and getuser() in Python (Password without echo)
- Python program to check the validity of a Password
- Python | Prompt for Password at Runtime and Termination with Error Message
- primary_key - Django Built-in Field Validation
- verbose_name - Django Built-in Field Validation
- help_text - Django Built-in Field Validation
- default - Django Built-in Field Validation
- error_messages - Django Built-in Field Validation
- null=True - Django Built-in Field Validation
- blank=True - Django Built-in Field Validation
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.