How would I validate an input to check if it contains at least 1 number, ONE CAPITAL letter and one lower case letter. I have looked through similar questions and they don't seem to be exactly what I need e.g only checking for numbers and rejecting letters, I need it to except anything with more than one number, capital letter and lower case letter.
Asked
Active
Viewed 1,452 times
-2
-
2https://stackoverflow.com/questions/7684815/regex-for-alphanumeric-with-at-least-1-number-and-1-character – user2341726 Aug 15 '17 at 14:36
-
3What have you tried so far? Please show some effort. – Moberg Aug 15 '17 at 14:37
-
https://stackoverflow.com/questions/41117733/validation-a-password-python duplicate? – Vinujan.S Aug 15 '17 at 14:40
-
I would suggest using regex to get your results, in the fastest possible way. I learned it through: [regexr.com] – BikerDude Aug 15 '17 at 14:40
-
1Maybe a regex could help? – BogdanC Aug 15 '17 at 14:41
2 Answers
0
You can use assert
combined with any()
and string
built-in module like this:
import string
password = '1Aa'
try:
assert any(i in string.ascii_lowercase for i in password)
assert any(i in string.ascii_uppercase for i in password)
assert any(i in string.digits for i in password)
except AssertionError as e:
raise Exception('Invalid password!')

ettanany
- 19,038
- 9
- 47
- 63
0
import re
def passwordagain():
password()
def password():
print()
password = input("Enter a secure password: ")
if re.search('[A-Z]',password) is None:
print("Password must contain at least one capital letter and one number.")
passwordagain()
elif re.search('[0-9]',password) is None:
print("Password must contain at least one capital letter and one number.")
passwordagain()
elif re.search('[a-z]',password) is None:
print("Password must contain at least one capital letter and one number.")
passwordagain()
else:
filename = ("password.");
with open (filename, "w") as f:
f.write (password)
print()
print("Password saved")

user8435959
- 165
- 1
- 11
-
-
-
That's not a very sensible way to initiate an infinite loop. You should look into the use of `while True:` - [this question and answer](https://stackoverflow.com/questions/3754620/a-basic-question-about-while-true) might help – asongtoruin Aug 15 '17 at 14:55