Login

Multiple emails form field

Author:
virhilo
Posted:
March 15, 2010
Language:
Python
Version:
1.1
Tags:
multiple email form field list
Score:
3 (after 3 ratings)

Field which accepts list of e-mail addresses separated by any character, except those which valid e-mail address can contain.

 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 <[email protected]>") by akaihola 4 years, 8 months ago
  2. email_links by sansmojo 9 years, 2 months ago
  3. Simple e-mail template tag by dchandek 8 years, 4 months ago
  4. Template Tag to protect the E-mail address by nitinhayaran 6 years, 7 months ago
  5. No Password E-mail by jefferya 7 years, 6 months ago

Comments

ludwik (on May 20, 2010):
<p>For Django 1.2 you need to replace</p> <pre>from django.forms.fields import email_re </pre> <p>with</p> <pre>from django.core.validators import email_re </pre>

#

kmike (on March 20, 2012):
<p>A simplier version that allows comma or semicolon-separated emails that can be used directly as "To:" value:</p> <pre>import re from django.core.validators import validate_email SEPARATOR_RE = re.compile(r'[,;]+') def validate_email_list(value): emails = SEPARATOR_RE.split(value) for email in emails: validate_email(email) </pre>

#

bastien (on October 25, 2012):
<p>I use a mix of both the snippet and Kmike's comment:</p> <p>import re</p> <p>from django.core.validators import email_re from django.forms import CharField, Textarea, ValidationError from django.utils.translation import ugettext as _ from django.core.validators import validate_email</p> <p>class EmailsListField(CharField):</p> <pre>widget = Textarea def clean(self, value): super(EmailsListField, self).clean(value) emails = re.compile(r'[^\w\.\-\+@_]+').split(value) if not emails: raise ValidationError(_(u'Enter at least one e-mail address.')) for email in emails: validate_email(email) return emails </pre> <p>the regex is more permissive, it works with Django 1.3, the logic allows for clear error messages although I don't use any with the validate_email() here one can be inserted.</p>

#

Please login first before commenting.