From b34cf054971e24801590bcc3f6e032e3ee6aadfa Mon Sep 17 00:00:00 2001 From: lza_menace Date: Wed, 24 Nov 2021 10:34:49 -0800 Subject: [PATCH] add simple email validator to form to prevent spam --- wowstash/forms.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/wowstash/forms.py b/wowstash/forms.py index a8c4337..745b66f 100644 --- a/wowstash/forms.py +++ b/wowstash/forms.py @@ -1,4 +1,6 @@ from re import match as re_match +from re import fullmatch as re_fullmatch +from re import compile as re_compile from flask_wtf import FlaskForm from wtforms import StringField, BooleanField from wtforms.validators import DataRequired, ValidationError @@ -11,6 +13,12 @@ class Register(FlaskForm): terms_reviewed = BooleanField('Terms Reviewed:', validators=[DataRequired()], render_kw={"class": "form-control-span"}) privacy_reviewed = BooleanField('Privacy Policy Reviewed:', validators=[DataRequired()], render_kw={"class": "form-control-span"}) + def validate_email(self, email): + regex = re_compile(r'([A-Za-z0-9]+[.-_])*[A-Za-z0-9]+@[A-Za-z0-9-]+(\.[A-Z|a-z]{2,})+') + if not re_fullmatch(regex, self.email.data): + raise ValidationError('This appears to be an invalid email. Contact admins if you feel this is incorrect.') + + class Login(FlaskForm): email = StringField('Email Address:', validators=[DataRequired()], render_kw={"placeholder": "Email", "class": "form-control", "type": "email"}) password = StringField('Password:', validators=[DataRequired()], render_kw={"placeholder": "Password", "class": "form-control", "type": "password"})