[Answered ]-Comparing two fields in django

2👍

Override your form’s clean method:

class MyRegistrationForm(forms.Form):
    password1=...
    password2=...
    ...

    def clean(self):
        cleaned_data = self.cleaned_data # individual field's clean methods have already been called
        password1 = cleaned_data.get("password1")
        password2 = cleaned_data.get("password2")
        if password1 != password2:
            raise forms.ValidationError("Passwords must be identical.")

        return cleaned_data

See the docs for more info.

You should also probably ALSO add some Javascript to check this on the client side – client side validation is no subsitute for server-side validation, but it is more responsive to the user and saves bandwidth.

Leave a comment