Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python program to check the validity of a Password?
Password validation is crucial for application security. A strong password should meet specific criteria including minimum length, mixed case letters, numbers, and special characters. Python's re module provides regular expressions to check these requirements efficiently.
Password Validation Rules
A valid password must satisfy the following criteria −
- Minimum 8 characters long
- At least one lowercase letter (a-z)
- At least one uppercase letter (A-Z)
- At least one digit (0-9)
- At least one special character (_ @ $)
- No whitespace characters
Algorithm
Step 1: Import re module for regular expressions Step 2: Get password input from user Step 3: Check minimum length (8 characters) Step 4: Verify lowercase letters using [a-z] Step 5: Verify uppercase letters using [A-Z] Step 6: Verify digits using [0-9] Step 7: Verify special characters [_@$] Step 8: Ensure no whitespace characters
Implementation
# Python program to check valid password
import re
password = input("Enter Password ::> ")
is_valid = True
while True:
if len(password) < 8:
is_valid = False
break
elif not re.search("[a-z]", password):
is_valid = False
break
elif not re.search("[A-Z]", password):
is_valid = False
break
elif not re.search("[0-9]", password):
is_valid = False
break
elif not re.search("[_@$]", password):
is_valid = False
break
elif re.search(r"\s", password):
is_valid = False
break
else:
print("This is a valid password!")
break
if not is_valid:
print("Not a valid password")
Example Output
Enter Password ::> vbnA@hj9 This is a valid password!
Enter Password ::> weak Not a valid password
Enhanced Function-Based Approach
import re
def validate_password(password):
"""Validate password against security criteria"""
# Check length
if len(password) < 8:
return False, "Password must be at least 8 characters long"
# Check for lowercase
if not re.search("[a-z]", password):
return False, "Password must contain lowercase letters"
# Check for uppercase
if not re.search("[A-Z]", password):
return False, "Password must contain uppercase letters"
# Check for digits
if not re.search("[0-9]", password):
return False, "Password must contain digits"
# Check for special characters
if not re.search("[_@$]", password):
return False, "Password must contain special characters (_ @ $)"
# Check for whitespace
if re.search(r"\s", password):
return False, "Password cannot contain whitespace"
return True, "Password is valid"
# Test the function
test_passwords = ["weak", "Strong123@", "NoSpecial123", "short@1A"]
for pwd in test_passwords:
is_valid, message = validate_password(pwd)
print(f"'{pwd}': {message}")
'weak': Password must be at least 8 characters long 'Strong123@': Password is valid 'NoSpecial123': Password must contain special characters (_ @ $) 'short@1A': Password is valid
Conclusion
Regular expressions provide an efficient way to validate password strength in Python. The re.search() method checks each security requirement systematically, ensuring robust password validation for applications.
Advertisements
