How to create a Django password validator?

Introduction

As the Django documentation says: “A validator is a callable that takes a value and raises a ValidationError if it doesn’t meet some criteria. Validators can be useful for reusing validation logic between different types of fiel…


This content originally appeared on DEV Community and was authored by DoriDoro

Introduction

As the Django documentation says: "A validator is a callable that takes a value and raises a ValidationError if it doesn’t meet some criteria. Validators can be useful for reusing validation logic between different types of fields."

Yes, you can make your own validator. In this example, I am going to create a password validator that will check if the given password contains at least one letter.

To create a custom validator, create a file called validators.py within one of your Django applications.

# validators.py

from django.core.exceptions import ValidationError


class ContainsLetterValidator:
    def validate(self, password, user=None):
        if not any(char.isalpha() for char in password):
            raise ValidationError(
                'The password must contain at least one letter',
                code='password_no_letters'
            )

    def get_help_text(self):
        return 'Your password must contain at least one upper or lower case letter.'

Once you have created the validator, add it to your settings.py file inside the AUTH_PASSWORD_VALIDATORS dictionary.

# settings.py


AUTH_PASSWORD_VALIDATORS = [
    {
        "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
        "NAME": "app.validators.ContainsLetterValidator",
    },
]

The validator is now used to validate the creation of a password.


This content originally appeared on DEV Community and was authored by DoriDoro


Print Share Comment Cite Upload Translate Updates
APA

DoriDoro | Sciencx (2024-09-03T15:54:21+00:00) How to create a Django password validator?. Retrieved from https://www.scien.cx/2024/09/03/how-to-create-a-django-password-validator/

MLA
" » How to create a Django password validator?." DoriDoro | Sciencx - Tuesday September 3, 2024, https://www.scien.cx/2024/09/03/how-to-create-a-django-password-validator/
HARVARD
DoriDoro | Sciencx Tuesday September 3, 2024 » How to create a Django password validator?., viewed ,<https://www.scien.cx/2024/09/03/how-to-create-a-django-password-validator/>
VANCOUVER
DoriDoro | Sciencx - » How to create a Django password validator?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/03/how-to-create-a-django-password-validator/
CHICAGO
" » How to create a Django password validator?." DoriDoro | Sciencx - Accessed . https://www.scien.cx/2024/09/03/how-to-create-a-django-password-validator/
IEEE
" » How to create a Django password validator?." DoriDoro | Sciencx [Online]. Available: https://www.scien.cx/2024/09/03/how-to-create-a-django-password-validator/. [Accessed: ]
rf:citation
» How to create a Django password validator? | DoriDoro | Sciencx | https://www.scien.cx/2024/09/03/how-to-create-a-django-password-validator/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.