emailvalidator django code example

Example 1: using regex validators in django models

from django.core.validators import RegexValidator
from django.db import models
PHONE_NUMBER_REGEX = RegexValidator(r'^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*$', 'only valid email is required')
class SomeClass(models.Model):
  phone =  models.CharField(max_length=14, validators=[PHONE_REGEX])

Example 2: email validation using django

from django import forms

class MyForm(forms.Form):
    even_field = forms.IntegerField(validators=[validate_even])

Example 3: email validation using django

from django.db import models

class MyModel(models.Model):
    even_field = models.IntegerField(validators=[validate_even])

Example 4: email validation using django

from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _

def validate_even(value):
    if value % 2 != 0:
        raise ValidationError(
            _('%(value)s is not an even number'),
            params={'value': value},
        )