Reputation: 1
I am setting up my server and I want to have the passwords with complexity.
Using a regex in Python, how can I verify that a user's password is:
At least 8 characters
Must be restricted to, though does not specifically require any of:
uppercase letters: A-Z
lowercase letters: a-z
numbers: 0-9
any of the special characters: @#$%^&+=
import re
password = raw_input("Enter string to test: ")
if re.match(r'[A-Za-z0-9@#$%^&+=]{8,}', password):
# match
else:
# no match
from wtforms import StringField, PasswordField, SubmitField, BooleanField
from wtforms.validators import DataRequired, Length, Email, EqualTo, ValidationError
class RegistrationForm(FlaskForm):
username = StringField('Username',
validators=[DataRequired(), Length(min=2, max=20)])
email = StringField('Email',
validators=[DataRequired(), Email()])
#password =
pattern = "^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$"
password = PasswordField('Password', validators=[DataRequired(), Length(min=2, max=20)])
if re.match(r'[A-Za-z0-9@#$%^&+=]{8,}', password):
print ("Valid password")
else:
print ("Password not valid")
I expect the output to be valid or invalid password
Upvotes: 0
Views: 441
Reputation: 19885
A regex seems a bit heavy to me. You can just use normal string operations:
from string import ascii_lowercase, digits
punctuation = '@#$%^&+='
valid_characters = ascii_lowercase + digits + punctuation
def validate_password(password):
return len(password) >= 8 and set(password) <= set(valid_characters)
Upvotes: 2