[Answered ]-Validating email format in Django

1πŸ‘

It is always safer and recommended to use the build-in email validator that comes with Django.

You have not put blank=True and null=True while declaring email. So, Django will always make sure that this field is never blank/null. Django will also make sure that only valid email address is entered by user. As a matter of fact, you need not do anything else.

email = forms.EmailField(max_length=50, widget=forms.EmailInput, label="Email")

Reference: https://docs.djangoproject.com/en/3.2/ref/forms/fields/#django.forms.Field.clean

0πŸ‘

if you want to modify email address in Django forms

email_user_part_regex = re.compile(
    r"^(?=.{4,30}$)[\w\u0080-\uffff]+(?:[\.\-+][\w\u0080-\uffff]+)*$",
    re.IGNORECASE | re.UNICODE,
)

class ForgetPasswordEmailForm(forms.Form):

email = forms.EmailField(label='email', max_length=200)

def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    validate_email.user_regex = email_user_part_regex
πŸ‘€achu prasad

Leave a comment