1

I want to make a password checker but how do I make it so I can write an error if there are characters other than digits, upper/lowercase and (,),$,%,_/.

What I have so far:

import sys
import re
import string
import random

password = input("Enter Password: ")
length = len(password)
if length < 8:
    print("\nPasswords must be between 8-24 characters\n\n")
elif length > 24:
    print ("\nPasswords must be between 8-24 characters\n\n")

elif not re.match('[a-z]',password):
        print ('error')
Brad Solomon
  • 38,521
  • 31
  • 149
  • 235
Pootato
  • 33
  • 1
  • 1
  • 4
  • 2
    https://regexone.com/ – Brad Solomon Oct 17 '17 at 20:29
  • Are you asking how to write a regex that matches the conditions you set? – thumbtackthief Oct 17 '17 at 20:35
  • 1
    This is a very helpful tool: https://regex101.com/ – thumbtackthief Oct 17 '17 at 20:36
  • You should learn the basics of regexes first. Open up your favourite search engine and search for something like "regex tutorial". – ForceBru Oct 17 '17 at 20:36
  • In addition to the above, you should use [`re.fullmatch`](https://docs.python.org/3/library/re.html#re.fullmatch) rather than `re.match` to match the whole string rather than just the beginning. – yinnonsanders Oct 17 '17 at 20:37
  • 1
    Here's a link that might be related to your question, https://stackoverflow.com/questions/41117733/validation-a-password-python – Aswini Oct 17 '17 at 20:38
  • Why not just make one regex for this and call it a day? Save yourself time, code, save your users time, sanity... `[a-zA-Z0-9()$%_]{8,24}`. There's nothing more annoying than a system prompting you multiple times for an incorrectly formatted password. Just present the user with all the conditions you want. `Password must meet the following requirements:\n- Length must be between 8 and 24 characters\n-Valid characters are ASCII letters (a-z or A-Z), ASCII numbers (0-9) or the following characters ()$%_`. Done. – ctwheels Oct 17 '17 at 20:51

4 Answers4

3

You need to have a regular expression against which you will validate like this:

m = re.compile(r'[a-zA-Z0-9()$%_/.]*$')
if(m.match(input_string)):
     # Do something...
else:
     # Reject with your logic ...
Maksiks
  • 94
  • 11
Gurvinder Singh
  • 91
  • 2
  • 10
1

Try

elif not re.match('^[a-zA-Z0-9()$%_/.]*$',password):

I can't tell if you want to allow commas. If so use ^[a-zA-Z0-9()$%_/.,]*$

thumbtackthief
  • 6,093
  • 10
  • 41
  • 87
MisterMystery
  • 402
  • 6
  • 14
1

If you want to avoid using RegEx, you can try this self-explanatory solution

allowed_characters=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','1','2','3','4','5','6','7','8','9','0','(',')','$','%','_','/']

password=input("enter password: ")
if any(x not in allowed_characters for x in password):
  print("error: invalid character")
else:
  print("no error")
Sim Son
  • 310
  • 1
  • 10
  • Very nice when you have few allowed characters and don't want to use RegEx. Thanks. – Alien426 Jun 12 '21 at 06:03
  • 1
    I would just replace `allowed_characters` with the built-in module `import string` where you can use `allowed_characters = string.ascii_letters + string.digits + r'()$%_/.]*$'` to be more readable. – Ricky Levi Sep 11 '22 at 19:37
0

With Python, you should be raising exceptions when something goes wrong:

if re.search(r'[^a-zA-Z0-9()$%_]', password):
    raise Exception('Valid passwords include ...(whatever)')

This searches for any character in the password that is not (^) in the set of characters defined between the square brackets.

Rob Bailey
  • 1,747
  • 15
  • 18