In this post, i am going to write complete source code for password validation system in python. As we know, Password prevent hackers or crackers from accessing our computers,personal emails and website login details. So password should be strong but easy to remember. We need to validate a password every time whenever a user creates an account on any website or app. So, we have to verify a valid password as well as put the confirm password validation. For a valid password, the following parameters must be contained by it

  • A password should be alphanumeric. it means it should contain alphabets and numbers
  • Password must contain at least one capital letter.
  • Password must contain a special character (@, $, !, &, etc).
  • Password length must be greater than 8 characters.
  • Password must contain uppercase and lowercase letter.

Whenever a user creates a password, there is always one more field of confirm password. It checks that the password entered by the user is same as this confirm password fields. To create a valid password, both the password and confirm password fields value must be matched and same. In this project,we will build password validation system that validate password using regular expression.

Password Validation using regular expression

To create a password validation system using Python you have to follow the steps mentioned below:

First we need to import re module from python library.

import re

Then we will print the message that contain above mentioned conditions which checks the validity of password and we will ask user to enter the password

print("Password should contains \n1) One Capital Letter\n2) Special Character\n3) One Number \n4) Length Should be 6-10: ")
pswd = input("Enter your password :")
reg = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!#%*?&]{6,10}$"

Try to understand the above line

(?=.*?[a-z])   # at least one lowercase letter
(?=.*?[A-Z])   # at least one uppercase letter
(?=.*?[0-9])   # at least one number
(?=.*[@$!%*#?&]) # at least one special character
[A-Za-z\d]     # only alphanumeric
{6,10}           # at least 6 to 10 characters long
$              # end word

We will use re.compile() method to compile regular expression pattern into regex pattern object. We can use this pattern object to search for a match inside password string by using re.serch() method. Using method,we will check if password contains the specified search pattern.

# compiling regex
match_re = re.compile(reg)

# searching regex
result =, pswd)

we will use if-else condition to check validity of password

# validating conditions
if result:
   print("Valid Password")
   print("Invalid Password")

In this way, we can validate the password using regular expression. Here is complete source code of password validation system. Check below

Password validation system using regular expression in python


>>> %Run 'password'
Password should contains 
1) One Capital Letter
2) Special Character
3) One Number 
4) Length Should be 6-10: 
Enter your password :Xya@34
Valid Password

>>> %Run 'password'
Password should contains 
1) One Capital Letter
2) Special Character
3) One Number 
4) Length Should be 6-10: 
Enter your password :athang16*
Invalid Password

Leave a Reply

Your email address will not be published.