Python program to take user input and check validity of a password

Python program to take user input and check 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 not valid, it will ask the user to re-enter the password. If valid , it will print one message and exit. Following condions should be true for a valid password :

1. The total character of the password should be equal or greater than 6 and equal or less than 12. That means 6<= length <=12.
2. It should contain atleast one lower case character in a to z
3. It should contain atleast one upper case character in A to Z
4. It should contain atleast one number in 1 to 9
5. It should contain atleast one character from ~!@#$%^&*
6. It should not contain any space , tab or any blank.

Let’s take a look at the python program to solve this problem :

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 letter 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")

Explanation :

The commented numbers in the above program denote the step number below :

1. Import re python module. This module is used for regex.
2. Run one infinite loop. This loop will run for infinite time till the password will become valid.
3. Ask the user to enter one password. Read and store it in variable user_input. is_valid is a flag used to determine if the current password is valid or not. False means it is not valid. We have set it as False in the beginning.
4. 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.
5. Check if the password contains any upper case character or not, else print one message and continue to the start of the loop.
6. Check if the password contains any lower case character or not, else print one message and continue to the start of the loop.
7. Check if the password contains any number or not, else print one message and continue to the start of the loop.
8. Check if the password contains any special character or not, else print one message and continue to the start of the loop.
9. Check if the password contains any blank space or not.If yes, print one message and continue to the start of the loop.
10. If all other cases are passed, mark is_valid as True and exit from the while loop.
11. If the password is valid, print one message to inform the user.

Sample Output :

Enter a password : abc123@
Not valid ! It should contain one letter between [A-Z]
Enter a password : abc1234567
Not valid ! It should contain one letter between [A-Z]
Enter a password : Abc1234567
Not valid ! It should contain at least one letter in [~!@#$%^&*]
Enter a password : Abc1234567@#$
Not valid ! Total characters should be between 6 and 12
Enter a password : ABC123@
Not valid ! It should contain one letter between [a-z]
Enter a password : Abc@#$
Not valid ! It should contain one letter between [1-9]
Enter a password : Abc123@
Password is valid

Leave a Reply