GoCoding.org

Program to validate password using regex

by | Dec 26, 2020 | Python Programs

Introduction

The task is to check whether the given password is valid or not using regular expression.

For a valid password there are a set of conditions to be checked:

  • Minimum 8 character is required.
  • There must be at least one upper case and one lower case alphabet.
  • The must be at least one digit between 0-9.
  • There must be at least one special character (_ or $ or @).

Program to validate password using regex

Program

Approach 1: Using regex

import re 
def validate(ip_pass):
    flag = 0
    while True:   
        if (len(ip_pass)<8): 
            flag = -1
            break
        elif not re.search("[A-Z]", ip_pass): 
            flag = -1
            break
        elif not re.search("[a-z]", ip_pass): 
            flag = -1
            break
        elif not re.search("[0-9]", ip_pass): 
            flag = -1
            break
        elif not re.search("[_@$]", ip_pass): 
            flag = -1
            break
        elif re.search("\s", ip_pass): 
            flag = -1
            break
        else: 
            flag = 0
            print("The input password is valid!") 
            break
    if flag ==-1: 
        print("The input password is invalid!") 
        
ip_pass = input("Enter the password to validate:")
validate(ip_pass)

Output 1

Program to validate password using regex Output 1

Approach 2: Using islower(), isupper(), isdigit()

def validate(ip_pass):
    lcase, ucase,digit,spcl = 0, 0, 0, 0
    if (len(ip_pass) >= 8): 
        for char in ip_pass:
# Count uppercase & lowercase alphabets, digits, special char 
            if (char.islower()): 
                lcase+=1            
            if (char.isupper()): 
                ucase+=1            
            if (char.isdigit()): 
                digit+=1            
            if(char=='@'or char=='$' or char=='_'): 
                spcl+=1           
    if (lcase>=1 and ucase>=1 and spcl>=1 and digit>=1 and lcase+spcl+ucase+digit==len(ip_pass)): 
        print("The input password is valid!")  
    else: 
        print("The input password is invalid!") 
        
ip_pass = input("Enter the password to validate:")
validate(ip_pass)

Output 2

Program to validate password using regex Output 2

Explanation

In the above python code, we have checked the above conditions for valid password using search() method in nested if-else statement. If all the conditions are satisfied, the input password is valid whereas, if any of the condition is false, the password is invalid.

Alternatively, we can count the number of lowercase and uppercase alphabets, digits and special characters. The count must be greater or equal to 1 for each and also the total character count must be greater or equal to 8. If the condition is satisfied, the input password is valid otherwise the password is invalid.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.