Search code examples
djangodjango-formsdjango-email

Default regex django uses to validate email


recently, I started playing with Django and created a custom form for user registration. In that form to create the field for email I use something like

email = forms.EmailField()

I observed that address such as [email protected] is considered invalid by the form. Of course this is a nonsense email address. Nonetheless, I wonder how does Django checks for validity.

I found some topics on the net discussing how to check for validity of an email address but all of them were providing some custom ways. Couldn't find something talking about the django default validator.

In their docs on the email filed they specify

Uses EmailValidator to validate that the given value is a valid email address, using a moderately complex regular expression.

However that's not very specific so I decided to ask here.


Solution

  • us should check docs here https://www.geeksforgeeks.org/emailfield-django-forms/#:~:text=EmailField%20in%20Django%20Forms%20is,max_length%20and%20min_length%20are%20provided.

    if u wanna check validation use clean function like this :

    from django.forms.fields import EmailField
    
    email = EmailField()
    
    my_email = "[email protected]"
    
    
    print(email.clean(my_email))
    

    if your email is valid then this func return value else return validation error