Skip to content

Instantly share code, notes, and snippets.

@klement97
Last active April 18, 2020 15:01
Show Gist options
  • Save klement97/fe3c1201836201024d89f23a25578750 to your computer and use it in GitHub Desktop.
Save klement97/fe3c1201836201024d89f23a25578750 to your computer and use it in GitHub Desktop.
import re
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext as _
class NumberValidator(object):
@staticmethod
def validate(password, user=None):
if not re.findall('\d', password):
raise ValidationError(
_("The password must contain at least 1 digit, 0-9."),
code='password_no_number',
)
@staticmethod
def get_help_text():
return _(
"Your password must contain at least 1 digit, 0-9."
)
class UppercaseValidator(object):
@staticmethod
def validate(password, user=None):
if not re.findall('[A-Z]', password):
raise ValidationError(
_("The password must contain at least 1 uppercase letter, A-Z."),
code='password_no_upper',
)
@staticmethod
def get_help_text():
return _(
"Your password must contain at least 1 uppercase letter, A-Z."
)
class LowercaseValidator(object):
@staticmethod
def validate(password, user=None):
if not re.findall('[a-z]', password):
raise ValidationError(
_("The password must contain at least 1 lowercase letter, a-z."),
code='password_no_lower',
)
@staticmethod
def get_help_text(self):
return _(
"Your password must contain at least 1 lowercase letter, a-z."
)
class SymbolValidator(object):
@staticmethod
def validate(password, user=None):
if not re.findall('[()[\]{}|\\`~!@#$%^&*_\-+=;:\'",<>./?]', password):
raise ValidationError(
_("The password must contain at least 1 symbol: " +
"()[]{}|\`~!@#$%^&*_-+=;:'\",<>./?"),
code='password_no_symbol',
)
@staticmethod
def get_help_text():
return _(
"Your password must contain at least 1 symbol: " +
"()[]{}|\`~!@#$%^&*_-+=;:'\",<>./?"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment