Quantcast
Channel: JS Tricks » Python
Viewing all articles
Browse latest Browse all 25

How to Validate email in Django

$
0
0

Email validation is very important for your web application. It helps you to filter out invalid emails before processing. Here we will learn how to Validate Email in Django.

How to validate email in Django

Django comes with very powerful features. So you can expect validation feature in Django.

You can validate email with the validate_email function which is in django.core.validators. Just import it in your script and pass the email parameter to the function.

Syntax

The syntax for validate_email is very easy. Have a look at that:

validate_email( email )

  • email – this is the email which you want to validate

Creating a function to validate email

You can create a function which will return True if the email is valid.

from django.core.validators import validate_email
from django.core.exceptions import ValidationError

def ValidateEmail( email ):    
    try:
        validate_email( email )
        return True
    except ValidationError:
        return False

Using this function to validate emails:

ValidateEmail('yahoo.com')
# returns False

ValidateEmail('jstricks@yahoo.com')
# returns True

I Hope, now you know how to Validate email in Django.

The post How to Validate email in Django appeared first on JS Tricks.


Viewing all articles
Browse latest Browse all 25

Trending Articles