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

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 not valid, it will ask the user to re-enter the password. If valid, it will print one message and exit.

Following conditions 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 at least one lower case character in a to z
  3. It should contain at least one upper case character in A to Z
  4. It should contain at least one number in 1 to 9
  5. It should contain at least 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 :

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

Python program :

python validate password

Explanation :

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

  1. Import re python module. This module is used for using regex in a program.
  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 userinput. _isvalid_ 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 at the beginning of the program.
  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. If it contains any upper case character, move to the next step.
  6. 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.
  7. 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.
  8. 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.
  9. Check if the password contains any blank space or not. If yes, print one message and continue to the start of the loop. If not, move to the next step.
  10. If all other cases are passed, mark isvalid_ as True and exit from the while loop, i.e. the password entered is valid.
  11. If the password is valid, print one message to inform the user.

Example :

python example validate password

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 verification.

View on Github

Similar tutorials :

Where is the color and why codevscolor ?

Long story short, I love paintings and I paint on weekends. We(me and my wife) have one Youtube channel. Below is a video that I did recently. If you love this please do subscribe to support us 😊