benpence/dontspamme — Issue #26: Email address is technically invalid
Author: Soares
Created: 2011-11-15 19:16:30+00:00
State: open

The email address regex in constraint.py is technically invalid. This is a personal pet peeve of mine.

Email addresses can contain unicode characters, spaces, escaped quotes, unicode characters, and multiple @ characters.

Furthermore, the domain can be specified as an IP address in brackets (nate@[127.0.0.1]) and some domains do not require a dot (nate@ai is a valid email address).

The most surefire way to 'validate' an email address is to check for at least one @ sign. The only real way to validate an email address is to an email and get validation.

If you want to catch some general typos, django has a decent regex:

email_re = 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
r')@(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?$', re.IGNORECASE) # domain
validate_email = EmailValidator(email_re, _(u'Enter a valid e-mail address.'), 'invalid')

But even this will refuse certain valid email addresses.