password_validation.py 979 B

123456789101112131415161718192021222324252627
  1. from django.core.exceptions import ValidationError
  2. from django.utils.translation import gettext as _
  3. class AlphanumericPasswordValidator:
  4. """
  5. Validate that the password has at least one numeral, one uppercase letter and one lowercase letter.
  6. """
  7. def validate(self, password, user=None):
  8. if not any(char.isdigit() for char in password):
  9. raise ValidationError(
  10. _("Password must have at least one numeral."),
  11. )
  12. if not any(char.isupper() for char in password):
  13. raise ValidationError(
  14. _("Password must have at least one uppercase letter."),
  15. )
  16. if not any(char.islower() for char in password):
  17. raise ValidationError(
  18. _("Password must have at least one lowercase letter."),
  19. )
  20. def get_help_text(self):
  21. return _("Your password must contain at least one numeral, one uppercase letter and one lowercase letter.")