我已经写了一个函数添加电子邮件到通讯基地。直到我增加了检查发送的电子邮件的有效性,它的工作完美无缺。现在每次我都收到“错误的邮件”作为回复。有人看到这里有什么错误吗?使用的正则表达式是:
\b[\w\.-]+@[\w\.-]+\.\w{2,4}\b
和它是100% 有效的(http://gskinner.com/RegExr/) ,但是我可能使用它是错误的,或者它可能是一些逻辑错误:
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