From d6721f27f7af09213f32469d2161d84827977caf Mon Sep 17 00:00:00 2001 From: xemeds Date: Sat, 18 Jul 2020 21:50:47 +0000 Subject: [PATCH] Made a better URL validator --- tiny0/forms.py | 10 ++++++++++ tiny0/routes.py | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/tiny0/forms.py b/tiny0/forms.py index 5c55626..6b57ae1 100644 --- a/tiny0/forms.py +++ b/tiny0/forms.py @@ -14,6 +14,16 @@ def validate_URL(form, field): # Raise a ValidationError raise ValidationError() + # If the url starts with a dot after http:// or after https:// or just starts with a dot + if field.data.startswith("http://.") or field.data.startswith("https://.") or field.data.startswith("."): + # Raise a ValidationError + raise ValidationError() + + # If the url ends with a dot and it is the only dot + if field.data.endswith(".") and field.data.count(".") == 1: + # Raise a ValidationError + raise ValidationError() + # If the URL does not start with http:// and https:// if not(field.data.startswith("http://")) and not(field.data.startswith("https://")): # Add https:// to the beginning of the URL diff --git a/tiny0/routes.py b/tiny0/routes.py index 359dc7a..a5cab03 100644 --- a/tiny0/routes.py +++ b/tiny0/routes.py @@ -22,6 +22,6 @@ def index(): # If the form was valid if form.validate_on_submit(): - return "Valid URL" + return "Valid URL: " + form.url.data return render_template("index.html", form=form)