1

Tried to use custom validation on django model based form via regex validation on password | ReType password using clean method form is working perfectly fine but clean isn't working out as expected can anyone please point out the mistake or shed some light on this topic as i'm stuck with it django.org ain't working out well for me, for your reference

ALSO if i've provided wrong password it easily stored it in database without raising the error

from django import forms
from .models import Person
from django.contrib.auth.password_validation import validate_password
from django.core.validators import ValidationError, RegexValidator
import re

class FormClass(forms.ModelForm):
    Password = forms.CharField(widget=forms.PasswordInput, required=True, validators=[RegexValidator(regex=r'[A-Za-z0-9@#$%^&+=]{8,}', message="Password should contain Upper, lower, digit and Special Character.")])
    ReType_Password = forms.CharField(widget=forms.PasswordInput, required=True, validators=[RegexValidator(regex=r'[A-Za-z0-9@#$%^&+=]{8,}', message="Password should contain Upper, lower, digit and Special Character.")])

    def clean_password(self):
        super(FormClass, self).clean()
        a = self.cleaned_data['Password']
        b = self.cleaned_data['ReType_Password']
        if a != b:
            raise ValidationError("Password not Matched")
        return self.cleaned_data

    class Meta:
        model = Person  
        fields = '__all__'
  • Does this answer your question? [Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters](https://stackoverflow.com/questions/19605150/regex-for-password-must-contain-at-least-eight-characters-at-least-one-number-a) – Robin Zigmond Jul 23 '20 at 18:24

0 Answers0