0

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
  • 34,372
  • 28
  • 129
  • 206
Pootato
  • 23
  • 1
  • 3
  • 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

2

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

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

another solution would be:

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
  • 312
  • 1
  • 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
  • 5,936
  • 8
  • 37
  • 80
MisterMystery
  • 392
  • 5
  • 14
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,657
  • 15
  • 18