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 FalseUsing this function to validate emails:
ValidateEmail('yahoo.com')
# returns False
ValidateEmail('jstricks@yahoo.com')
# returns TrueI Hope, now you know how to Validate email in Django.
The post How to Validate email in Django appeared first on JS Tricks.