Multiple emails form field

 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
import re

from django.forms.fields import email_re
from django.forms import CharField, Textarea, ValidationError
from django.utils.translation import ugettext as _


email_separator_re = re.compile(r'[^\w\.\-\+@_]+')
     
                                                                                        
def _is_valid_email(email):    
    return email_re.match(email)


class EmailsListField(CharField):

    widget = Textarea

    def clean(self, value):
        super(EmailsListField, self).clean(value)

        emails = email_separator_re.split(value)

        if not emails:
            raise ValidationError(_(u'Enter at least one e-mail address.'))

        for email in emails:
            if not _is_valid_email(email):
                raise ValidationError(_('%s is not a valid e-mail address.') % email)

        return emails

More like this

  1. Validation for full e-mails (e.g. "Joe Hacker <joe@hacker.com>") by akaihola 1 month, 2 weeks ago
  2. email_links by sansmojo 4 years, 8 months ago
  3. Simple e-mail template tag by dchandek 3 years, 9 months ago
  4. No Password E-mail by jefferya 2 years, 11 months ago
  5. Template Tag to protect the E-mail address by nitinhayaran 2 years ago

Comments

ludwik (on May 20, 2010):

For Django 1.2 you need to replace

from django.forms.fields import email_re

with

from django.core.validators import email_re

#

(Forgotten your password?)