Trending Articles       10 Flutter Listview Examples       Flutter Custom Button Example       Flutter Audio Recording       Flutter 2.0 Autocomplete TextField       Current Location Google Maps

Password validation in python with regex

When we are going to make any user authentication more secure, user has to provide secure password while creating user account. When user provide strong password, then account should be more secure. In this python example we are showing Password validation with regex. To use regex in python we need to import "re" module to our python example.

This example we are validating the password with regex equation which will validate below conditions

  1. One Capital Letter
  2. One Number
  3. Special Character
  4. Length Should be 8-18

Let's write python regex example to validate password

 

import re

# input
print("Please enter password should be \n1) One Capital Letter\n2) Special Character\n3) One Number \n4) Length Should be 8-18: ")
pswd = input()


reg = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!#%*?&]{8,18}$"

# compiling regex
match_re = re.compile(reg)

# searching regex
res = re.search(match_re, pswd)

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

 

Steps to execute program

step 1: We are taking the password text as input from user.

step 2: creating the regex  compiler equation with given validation string

step 3: "re" module search() method we are passing the reg expression and user password input. This re.search() will be validate given string matches to the corresponding regex condition or not and return true/false response.

step 4: Based on this response we are printing the validate password result.

 

Output:

Example 1:

Please enter password should be
1) One Capital Letter
2) Special Character
3) One Number
4) Length Should be 8-18:
asd@Asda
Invalid Password

 

Example 2:

 

Please enter password should be
1) One Capital Letter
2) Special Character
3) One Number
4) Length Should be 8-18:
asd@Asd1
Valid Password

 

Related

Python String replacement with regex example

 

 


Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

151 Views

Flutter Questions

Android Questions