Validation for full e-mails (e.g. "Joe Hacker <joe@hacker.com>")

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
### full_email/__init__.py #####################################################

"""Full e-mail address validation for Django"""


### full_email/formfields.py ###################################################

from django import forms

from .validators import validate_full_email


class FullEmailField(forms.EmailField):
    default_validators = [validate_full_email]


### full_email/validators.py ###################################################

from django.core.validators import email_re, EmailValidator, ValidationError
from django.utils.translation import ugettext_lazy as _


class FullEmailValidator(EmailValidator):
    def __call__(self, value):
        try:
            super(FullEmailValidator, self).__call__(value)
        except ValidationError, e:
            # Trivial case failed. Try for possible Full Name <email@address>
            if value and u'<' in value and value.endswith(u'>'):
                parts = value.rsplit(u'<', 1)
                fullname_part = parts[0].rstrip()
                email_part = parts[1][:-1]
                super(EmailValidator, self).__call__(email_part)
            else:
                raise


validate_full_email = FullEmailValidator(email_re,
                                         _(u'Enter a valid e-mail address.'),
                                         'invalid')


### full_email/models.py #######################################################

from django.db import models
from . import formfields
from .validators import validate_full_email


class FullEmailField(models.EmailField):
    default_validators = [validate_full_email]

    def formfield(self, **kwargs):
        defaults = {'form_class': formfields.FullEmailField}
        defaults.update(kwargs)
        return super(FullEmailField, self).formfield(**defaults)

More like this

  1. email_links by sansmojo 5 years, 11 months ago
  2. Multiple emails form field by virhilo 3 years, 2 months ago
  3. Template Tag to protect the E-mail address by nitinhayaran 3 years, 3 months ago
  4. SeparatedValuesField by jezdez 5 years, 5 months ago
  5. duplicate model object merging script by nstrite 5 years, 9 months ago

Comments

Cross20ALLISON (on December 30, 2011):

A lot of specialists say that loan aid people to live their own way, just because they can feel free to buy necessary goods. Furthermore, a lot of banks offer auto loan for young and old people.

#

(Forgotten your password?)