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 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:

  1. The total character of the password should be equal to or greater than 6 and equal to or less than 12.
  2. It should contain at least one lowercase character.
  3. It should contain at least one uppercase character.
  4. It should contain at least one non-zero digit.
  5. It should contain at least one special character from ~!@#\$%^&\*
  6. It should not contain any space, tab or any other blank 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

python validate password

Explanation :

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

  1. Import the re module. This module is used for using regex in a program.
  2. 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.
  3. Ask the user to enter one password. Read the value and assign it to the variable user_input. The boolean variable is_valid is a flag used to determine if the current password is valid or not. We are assigning False to this variable at the start 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 spaces 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, assign True to the variable is_valid and exit from the while loop, i.e. the password entered is valid.
  11. If the password is valid, print one message to 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 validation for any password.

Similar tutorials :