Last Updated : 06 Sep, 2024
In this program, we will be taking a password as a combination of alphanumeric characters along with special characters, and checking whether the password is valid or not with the help of a few conditions.
Primary conditions for password validation:
Examples:
Input : R@m@_f0rtu9e$Way 1:
Output : Valid PasswordInput : Rama_fortune$
Output : Invalid Password
Explanation: Number is missingInput : Rama#fortu9e
Output : Invalid Password
Explanation: Must consist from _ or @ or $
Here we have used the re module that provides support for regular expressions in Python. Along with this the re.search() method returns False (if the first parameter is not found in the second parameter) This method is best suited for testing a regular expression more than extracting data. We have used the re.search() to check the validation of alphabets, digits, or special characters. To check for white spaces we use the "\s" which comes in the module of the regular expression.
Python
# Python program to check validation of password
# Module of regular expression is used with search()
import re
password = "R@m@_f0rtu9e$"
flag = 0
while True:
if (len(password)<=8):
flag = -1
break
elif not re.search("[a-z]", password):
flag = -1
break
elif not re.search("[A-Z]", password):
flag = -1
break
elif not re.search("[0-9]", password):
flag = -1
break
elif not re.search("[_@$]" , password):
flag = -1
break
elif re.search("\s" , password):
flag = -1
break
else:
flag = 0
print("Valid Password")
break
if flag == -1:
print("Not a Valid Password ")
Time complexity: O(n), where n is the length of the password string.
Auxiliary space: O(1), as we are using only a few variables to store intermediate results.
l, u, p, d = 0, 0, 0, 0
s = "R@m@_f0rtu9e$"
if (len(s) >= 8):
for i in s:
# counting lowercase alphabets
if (i.islower()):
l+=1
# counting uppercase alphabets
if (i.isupper()):
u+=1
# counting digits
if (i.isdigit()):
d+=1
# counting the mentioned special characters
if(i=='@'or i=='$' or i=='_'):
p+=1
if (l>=1 and u>=1 and p>=1 and d>=1 and l+p+u+d==len(s)):
print("Valid Password")
else:
print("Invalid Password")
Time complexity: O(n) where n is the length of the input string s.
Auxiliary space: O(1) as it only uses a few variables to store the count of various characters.
Way 3: Without using any built-in method
Python
l, u, p, d = 0, 0, 0, 0
s = "R@m@_f0rtu9e$"
capitalalphabets="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
smallalphabets="abcdefghijklmnopqrstuvwxyz"
specialchar="$@_"
digits="0123456789"
if (len(s) >= 8):
for i in s:
# counting lowercase alphabets
if (i in smallalphabets):
l+=1
# counting uppercase alphabets
if (i in capitalalphabets):
u+=1
# counting digits
if (i in digits):
d+=1
# counting the mentioned special characters
if(i in specialchar):
p+=1
if (l>=1 and u>=1 and p>=1 and d>=1 and l+p+u+d==len(s)):
print("Valid Password")
else:
print("Invalid Password")
Time complexity : O(n), where n is the length of the input string s.
Auxiliary space : O(1), as here only few variables are used to store the intermediate results.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4