Reputation: 33655
Using Django 1.8 model EmailField
like so...
email = models.EmailField(verbose_name='email address', max_length=254, unique=True, db_index=True)
...allows invalid emails to be entered. For example the following creates a valid user when should error...
User(email="number_six@caprica", password="xyz")
However, if I test the validator in Django it correctly catches it...
from django.core.validators import validate_email validate_email("number_six@caprica")
I get the correct Enter a valid email address.
responce.
So whats going on? Does EmailField
and validate_email
not use the same regex? Why does EmailField
accept invalid email addresses where validate_email
does not?
Upvotes: 2
Views: 1220
Reputation: 5574
These are not invalid email addresses, browsers accept those too in inputs of type email.
me@localhost
for example is a valid email address.
Local domain names do not require a TLD, that is why.
Edit: although these are valid email addresses, Django does not accept them. EmailField
does use the validate_email
validator.
Your issue is that model instances validation has to be run manually, using the full_clean
method for example. See https://docs.djangoproject.com/en/1.8/ref/models/instances/#validating-objects for more information.
Upvotes: 2