simple email validation function

1
2
3
4
5
6
# better solution proposed by KpoH:

from django.forms.fields import email_re

def is_valid_email(email):
    return True if email_re.match(email) else False

More like this

  1. MultipleEmailsField by spanasik 4 years, 1 month ago
  2. Make hyperlinks for labels of raw_id_fields (jQuery) by ramen 3 years, 4 months ago
  3. FuzzyDateTimeField by japerk 4 years, 1 month ago
  4. Confirm alert if the user navigates away without saving changes by mrazzari 3 years, 10 months ago
  5. Decoupling models with cross-database relations by zvikico 2 years, 3 months ago

Comments

KpoH (on September 29, 2008):

better solution

from django.forms.fields import email_re

if email_re.match(email):
    return True
return False

#

KpoH (on September 29, 2008):

even better :)

from django.forms.fields import email_re

def is_valid_email(email):
    return True and email_re.match(email) or False

or

def is_valid_email(email):
    return True if email_re.match(email) else False

#

flavio87 (on September 29, 2008):

hey there thanks.

can you please repost that in a new snippet?

just realized my code is not even working properly.

sorry about that

#

flavio87 (on September 29, 2008):

nevermind about reposting, i just put in your code

#

mksoft (on November 3, 2008):

This is cleaner:

from django.forms.fields import email_re

def is_valid_email(email):
    return bool(email_re.match(email))

but a function is redundant, instead of calling it each time, one can simply do:

if email_re.match(email):
    ...

#

hunterford (on January 14, 2010):

Just a note for those on Django 1.1.1 or higher having trouble getting this to work...

The location of the regular expression has moved to:

from django.core.validators import email_re

#

(Forgotten your password?)