Email Validation and Formatting Function

  • Share this:

Code introduction


This function takes an email address as an argument, uses Django's core validator to check if the email address format is valid, if valid, it converts it to uppercase. If the format is invalid, it returns an error message.


Technology Stack : Django, validate_email

Code Type : Function

Code Difficulty : Intermediate


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

def validate_and_format_email(email):
    try:
        validate_email(email)
        return email.upper()
    except ValidationError:
        return "Invalid email format"