from django.core.exceptions import ValidationError from django.utils.translation import gettext as _ class ComplexityPasswordValidator: """Validate that the password contains letters, digits and special characters. Rules: - At least one letter (a-z or A-Z) - At least one digit (0-9) - At least one special character (any non-alphanumeric) """ def validate(self, password, user=None): has_letter = any(ch.isalpha() for ch in password) has_digit = any(ch.isdigit() for ch in password) has_special = any(not ch.isalnum() for ch in password) if not (has_letter and has_digit and has_special): raise ValidationError( _("This password must include at least one letter, one digit, and one special character."), code='password_not_complex_enough', ) def get_help_text(self): return _("Your password must include at least one letter, one digit, and one special character.")