Django EmailField point before @

I have the same little problem with django EmailField.

forms.EmailField(required=True, label="E-mail", widget=forms.TextInput(attrs=

      

He did not accept email in the form

xx. @ Xxxxxxx.xxx

Drops an incorrectly shaped error message, but mail usually works (email 15 years). I think the problem before the sign is the problem.

This is mistake? Is there any solution to make it work for emails like this?

thanks a lot:)

+3


source to share


3 answers


In RFC 5321 , section 4.2.1, the "local part" of an 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 Atom must have at least one character, and Dot-string is one or more Atoms with dots in between, and Local-part is either Dot-string or Quoted-string.



If I interpret this correctly, then the atom must always follow the dot, your email address is officially invalid and, for example, two dots in a line are also invalid.

Even though servers like GMail prefer to simply filter out all points in the local part and accept those addresses, which does not make them officially valid.

Setting the local part ( "xx."@xxxx.xxx

) should work, but you can also write your own validator and your own EmailField subclass.

+2


source


EmailField

- this CharField

which verifies that it is a valid email address. It uses EmailValidator to validate the input.

This class has EmailValidator

broken your email along this line:

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

      



Thus, it user_part

is part of before @

. and EmailValidator

check if it's correct with 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 is no option \.

before @

, there is one \.

that follows [-!#$%&'*+/=?^_`{}|~0-9A-Z]+

! therefore there is no possibility for \.

!!!

+2


source


As others have said, it may not be a valid email address. But as a workaround, you could do something like this. I haven't included regex, but I hope 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 take a look at django.core.validators.EmailValidator for an idea of ​​how to create a regular expression.

0


source







All Articles