I have written a function for adding emails to newsletter base. Until I've added checking validity of sent email it was working flawlessly. Now each time I'm getting "Wrong email" in return. Can anybody see any errors here ? The regex used is :
\b[\w\.-]+@[\w\.-]+\.\w{2,4}\b
and it is 100% valid (http://gskinner.com/RegExr/), but I may be using it wrong, or it may be some logic error :
def newsletter_add(request):
if request.method == "POST":
try:
e = NewsletterEmails.objects.get(email = request.POST['email'])
message = _(u"Email is already added.")
type = "error"
except NewsletterEmails.DoesNotExist:
if validateEmail(request.POST['email']):
try:
e = NewsletterEmails(email = request.POST['email'])
except DoesNotExist:
pass
message = _(u"Email added.")
type = "success"
e.save()
else:
message = _(u"Wrong email")
type = "error"
import re
def validateEmail(email):
if len(email) > 6:
if re.match('\b[\w\.-]+@[\w\.-]+\.\w{2,4}\b', email) != None:
return 1
return 0