[Django]-Django EmailField point before @

2👍

In RFC 5321, section 4.2.1, the "local-part" of the address has the following grammar:

Local-part     = Dot-string / Quoted-string
                 ; MAY be case-sensitive


Dot-string     = Atom *("."  Atom)

Atom           = 1*atext

The way I interpret this is that an Atom must have at least one character, and a Dot-string is one or more Atoms with dots in between them, and a Local-part is either a Dot-string or a Quoted-string.

If I interpret that correctly, then an atom must always follow after a dot, your email address is officially invalid, and e.g. two dots in a row is also invalid.

Even though servers like GMail choose to just filter out all the dots in the local part and accept those addresses, that doesn’t make them officially valid.

Quoting the local part ("xx."@xxxx.xxx) should work, but you could also write a custom validator and your own subclass of the EmailField.

2👍

EmailField is a CharField that checks that the value is a valid email address. It uses EmailValidator to validate the input.

That EmailValidator class split your email by this line :

user_part, domain_part = value.rsplit('@', 1) 

Thus user_part is the part before @ . and EmailValidator check the validation of it by this regex :

user_regex = re.compile(
    r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*$"  # dot-atom
    r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-\011\013\014\016-\177])*"$)',  # quoted-string
    re.IGNORECASE)

As you can see there in no possibility for \. before @, there is one \. that [-!#$%&'*+/=?^_`{}|~0-9A-Z]+ comes after it ! so there is no chance for \. !!!

👤Mazdak

0👍

As the others said, maybe that isn’t a valid email address. But as a workaround you could do something like this. I haven’t put the regular expression in, but hopefully you get the idea:

from django.core.validators import EmailValidator
from django.db import models

class MyEmailValidator(EmailValidator):
    user_regex = re.compile('ALTERED REGULAR EXPRESSION')

class MyModel(models.Model):
    forms.EmailField(required=True, label="E-mail", validators=[MyEmailValidator()])

You can have a look at django.core.validators.EmailValidator for ideas about how to craft the regular expression.

Leave a comment