Python program to take user input and check the validity of a password:
In this tutorial, we will learn how to check the validity of a user-input password in Python. The user will enter one password and our program will check if it is valid or not. If the password is not valid, it will ask the user to re-enter the password. If it is valid, it will print that the password is valid.
The following conditions should be true for a valid password:
- The total character of the password should be equal to or greater than 6 and equal to or less than 12.
- It should contain at least one lowercase character.
- It should contain at least one uppercase character.
- It should contain at least one non-zero digit.
- It should contain at least one special character from
~!@#\$%^&\*
- It should not contain any
space
,tab
or any otherblank space
.
Python program to check if a given password is valid or not:
We will use the regular expression module re
to validate different conditions. Let’s take a look at the Python program:
#1
import re
#2
while True:
#3
user_input = input("Enter a password : ")
is_valid = False
if (len(user_input)<6 or len(user_input)>12):
#4
print("Not valid! Total characters should be between 6 and 12")
continue
elif not re.search("[A-Z]",user_input):
#5
print("Not valid! It should contain one letter between [A-Z]")
continue
elif not re.search("[a-z]",user_input):
#6
print("Not valid! It should contain one letter between [a-z]")
continue
elif not re.search("[1-9]",user_input):
#7
print("Not valid! It should contain one digit between [1-9]")
continue
elif not re.search("[~!@#$%^&*]",user_input):
#8
print("Not valid! It should contain at least one letter in [~!@#$%^&*]")
continue
elif re.search("[\s]",user_input):
#9
print("Not valid! It should not contain any space")
continue
else:
#10
is_valid = True
break
#11
if(is_valid):
print("Password is valid")
Download the program on Github
Explanation :
The commented numbers in the above program denote the step number below :
- Import the
re
module. This module is used for using regex in a program. - Run one infinite loop. This loop will run for an infinite time until the user enters a valid password. If the user enters any invalid password, it will ask the user to enter the password again.
- Ask the user to enter one password. Read the value and assign it to the variable
user_input
. The boolean variableis_valid
is a flag used to determine if the current password is valid or not. We are assigningFalse
to this variable at the start of the program. - Check if the length of the password is between 6 to 12 or not. If not, print one message and continue running the while loop again, i.e. ask the user to enter a new password again.
- Check if the password contains any upper case character or not, else print one message and continue to the start of the loop. If it contains any upper case character, move to the next step.
- Check if the password contains any lower case character or not, else print one message and continue to the start of the loop. If it does, move to the next step.
- Check if the password contains any number or not, else print one message and continue to the start of the loop. It any number is found, move to the next step.
- Check if the password contains any special character or not, else print one message and continue to the start of the loop. If any special character is found, move to the next step.
- Check if the password contains any blank spaces or not. If yes, print one message and continue to the start of the loop. If not, move to the next step.
- If all other cases are passed, assign
True
to the variableis_valid
and exit from thewhile
loop, i.e. the password entered is valid. - If the password is valid, print one message to the user.
Example :
Note that the password validation checks may differ for your application. You can use a series of if-elif-else conditions or you can write one regex to do the validation for any password.
Similar tutorials :
- Python tutorial to check if a user is eligible for voting or not
- How to read a user input list in python
- How to find the md5 hash of a string in python
- Python program to find all numbers in a string
- Python find the key of a dictionary with maximum value
- Python program to find the cube sum of first n numbers