This post contains a total of 10+ Python Password Validator Program Examples with Source Code. All these programs to Validate Passwords are made using Python.
You can use the source code of these examples with credits to the original owner.
Related Posts
Python Password Validator Programs
1. By Ayush Sinha
Made by Ayush Sinha. Program that validates passwords to match specific rules. For example, the minimum length of the password must be eight characters long and it should have at least one uppercase letter in it. Source
qwerty is a valid password : False
from string import punctuation
def Validator(x):
n = (i for i in range(0,11))
if ( (" " not in x) and ( len(x) >= 5 and len(x) <= 10)):
for k in n:
if (str(k) in x):
for i in punctuation:
if i in x:
return True
return False
p = input()
print(f"{p} is a valid password : {Validator(p)}")
2. By michal
Made by michal. A single line Python Password validator program. Source
qwe123!! True
print((lambda pw: len(pw)>=5 and len(pw)<=10 and " " not in pw and any(c in "0123456789" for c in pw) and any(c in "#β¬%&*-+()<>$Β§=:;,_.!?/@|~'\"\\" for c in pw))(input()))
3. By David Carroll
Made by David Carroll. RegEx Password Validator. Source
Input: "$ololearn7" Output: True Input: "7ololearn$" Output: True Input: "olol$earn7" Output: True Input: "olol7earn$" Output: True Input: "Sololearn" Output: False Input: "John Doe" Output: False Input: "$ololearn" Output: False Input: "7ololearn" Output: False Input: "ololearn7" Output: False Input: "ololearn$" Output: False
import re
passwords = [
'$ololearn7'
,'7ololearn$'
,'olol$earn7'
,'olol7earn$'
,'Sololearn'
,'John Doe'
,'$ololearn'
,'7ololearn'
,'ololearn7'
,'ololearn$'
]
status_labels = ["false", "true"]
#Original Regex:
# ^(?=[^\s]*\d)(?=[^\s\d]*\W)[^\s]{5,10}$
password_rules = r'(?=[^\s]*\d)(?=[^\s]*\W)^[^\s]{5,10}$'
for password in passwords:
print(f'Input: "{password}"\nOutput: {re.match(password_rules, password ) != None}\n')
4. By Anna
Made by Anna. Simple Password Validator. Source
Sololearn: invalid John Doe: invalid $ololearn7: valid
from string import digits, punctuation
def validate_password(p):
if not 5 <= len(p) <= 10:
return False
if not any(c in digits for c in p):
return False
if not any(c in punctuation for c in p):
return False
if ' ' in p:
return False
return True
for p in ('Sololearn', 'John Doe'
, '$ololearn7'):
print(p, ': ', ('invalid', 'valid')[validate_password(p)], sep='')
5. By CΓ©pagrave
Made by CΓ©pagrave. Another Password validator program using RegEx. Source
ypass -> Not Valid $ololear7 -> Valid $olol arn7 -> Not Valid sololearn -> Not Valid $ololearn -> Not Valid sololear7 -> Not Valid $ol7 -> Not Valid $ololear7$ololear7 -> Not Valid More details : " Mypass ": -> doesn't contain any digit, -> doesn't contain any special character, " $ololear7 ": -> is valid " $olol arn7 ": -> contains white(s) space(s), " sololearn ": -> doesn't contain any digit, -> doesn't contain any special character, " $ololearn ": -> doesn't contain any digit, " sololear7 ": -> doesn't contain any special character, " $ol7 ": -> doesn't have 5 to 10 characters, " $ololear7$ololear7 ": -> doesn't have 5 to 10 characters,
import re
spChar = "!\"#$%&'()*+,-./:;<=>[email protected][\]^_`|{}~"
# passwords to check:
pws = ( input(),
'$ololear7',
'$olol arn7',
'sololearn',
'$ololearn',
'sololear7',
'$ol7',
'$ololear7$ololear7',)
pat = "^(?=.*\d)(?!.*\s)(?=.*["+spChar+"]).{5,10}$"
for pw in pws:
print(f"{pw:18} -> {'Valid'*bool(re.search(pat,pw))or'Not Valid'}")
# detailed version:
print('\nMore details :\n')
rules = {
'(?=^.{5,10}$)': " -> doesn't have 5 to 10 characters,",
'(?=.*\d)': " -> doesn't contain any digit,",
'(?!.*\s)': " -> contains white(s) space(s),",
"(?=.*["+spChar+"])": " -> doesn't contain any special character,"
}
for pw in pws:
print(f'" {pw} ":\n','\n'.join([rules[r].format(pw)for r in rules.keys() if not re.match(r,pw)])or ' -> is valid',sep='')
print()
6. By Nova
Made by Nova. Simple Program To Validate Password Using Python. Source
>>> Minimum Length Of Password : 5 >>> Maximum Length Of Password : 10 >>> Password Should Contain Numbers : 0 1 2 3 4 5 6 7 8 9 >>> Password Should Contain Special Characters : & + @ $ # % >>> Password Should Not Contain Blank Spaces Enter Your Password : Mypass123$%^ The Maximum Length Of Password Should Be 10 !!!
def intro(min_len,max_len,number,special):
print("Password Validator Using Python")
print(">>> Minimum Length Of Password : ",min_len)
print(">>> Maximum Length Of Password : ",max_len)
print(">>> Password Should Contain Numbers : ",end="")
for x in number:
print(x," ",end="")
print("\n>>> Password Should Contain Special Characters : ",end="")
for x in special:
print(x," ",end="")
print("\n>>> Password Should Not Contain Blank Spaces")
min_len = 5
max_len = 10
number = ('0','1','2','3','4','5','6','7','8','9')
special = ('&','+','@','$','#','%')
flag_no = 0
flag_sp = 0
flag__ = 0
flag_final = 0
intro(min_len,max_len,number,special)
password = input("\nEnter Your Password : ")
print(password)
lst = [x for x in password]
for i in lst:
if i in number:
flag_no += 1
if i in special:
flag_sp += 1
if i is ' ':
flag__ += 1
if len(lst) < min_len :
print("The Minimum Length Of Password Should Be ",min_len," !!!")
flag_final += 1
if len(lst) > max_len :
print("The Maximum Length Of Password Should Be ",max_len," !!!")
flag_final += 1
if flag_no is 0:
print("Enter Atleast One Number In Your Password !!!")
flag_final += 1
if flag_sp is 0:
print("Enter Atleast One Special Character In Your Password !!!")
flag_final += 1
if flag__ is not 0:
print("Remove The Spaces From Your Password !!!")
flag_final += 1
print("\nRESULT : ",end="")
if flag_final is 0:
print("Your Entered Password Is Perfectly Validated.")
else:
print("So Please Make The Necessary Changes So Your Password Can Be Validated And Accepted ")
7. By Giannis Macheras
Made by Giannis Macheras. Source
Your password: pass123 Password Security Level: Weak
import re
password = input()
with_uppercase = re.match(r'.*[A-Z]+.*', password)
with_lowercase = re.match(r'.*[a-z]+.*', password)
with_numbers = re.match(r'.*[0-9]+.*', password)
with_symbols = re.match(r'.*[@#$%^&!;+-.,/\|]+.*', password)
adequate_length = len(password) >= 8
conditions_passing = len(list(filter(bool,[with_uppercase, with_lowercase, with_numbers, with_symbols, adequate_length])))
results = ["Extremely Weak", "Very Weak", "Weak", "Medium", "Strong", "Very Strong"]
print("Your password:", password)
print("Password Security Level:", results[conditions_passing])
8. By Niki
Made by Niki. Source
Mypass1& Valid password
passw=input()
if len(passw) in range(5,11) and any(j.isdigit() for j in passw) and any(spch in " [@_!#$%^&*()<>?/|}{~:].;/\|~`\"" for spch in passw) and " " not in passw:
print("Valid password")
else:
print("Invalid password.\nβ Read the rules in the comment.β " )
9. By Mohamed Arshad
Made by Mohamed Arshad. Source
Enter a password: asdf <-----The given password is invalid
password=input("Enter a password:")
if(password[0].isdigit())and(len(password)>=6):
print("",password,"<-----The given password is valid and has been accepted")
else:
print("",password,"<-----The given password is invalid")
10. By Alice
Made by Alice. Basic Password validator program. Source
INPUT: qwerty False
s = input("INPUT: ")
print("{}\n{}".format(s, 5 <= len(s) <= 10 and any(l in "0123456789" for l in s) and any(l in "!\"#$%&'()*+,-./:;<=>[email protected][\]^_`{|}~" for l in s) and not " " in s))
11. By lolo
Made by lolo. Source
Your password: " Pass123&& " is valid !
import re
import string
letters=string.ascii_letters
digits=string.digits
punc=string.punctuation
letterUpper=False
letterLower=False
letterDigits=False
letterPunc=False
repeatChar=False
psw=input()
if len(psw)>=8 and len(psw)<15 :
for i in range(len(psw)):
if i<len(psw)-2:
if psw[i]==psw[i+1] and psw[i]==psw[i+2]:repeatChar=True
if re.search(psw[i],letters.upper()): letterUpper =True
elif re.search(psw[i],letters.lower()): letterLower =True
elif re.search(psw[i],digits): letterDigits =True
elif re.search(psw[i],punc): letterPunc =True
if letterUpper and letterLower and letterDigits and letterPunc and repeatChar==False:
print("Your password: \"", psw,"\" is valid !")
else:
print("Your password: \"", psw,"\" is not valid !\n")
if repeatChar==True: print("Repeated characters more than 2 times following")
if letterUpper==False: print("Missing capital letter")
if letterLower==False: print("Missing tiny letter")
if letterDigits==False: print("Missing digit")
if letterPunc==False: print("Missing punctuation")
else:print("Please enter a password between 8-15 characters")