Made a better URL validator
This commit is contained in:
@@ -14,6 +14,16 @@ def validate_URL(form, field):
|
|||||||
# Raise a ValidationError
|
# Raise a ValidationError
|
||||||
raise 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 the URL does not start with http:// and https://
|
||||||
if not(field.data.startswith("http://")) and not(field.data.startswith("https://")):
|
if not(field.data.startswith("http://")) and not(field.data.startswith("https://")):
|
||||||
# Add https:// to the beginning of the URL
|
# Add https:// to the beginning of the URL
|
||||||
|
|||||||
+1
-1
@@ -22,6 +22,6 @@ def index():
|
|||||||
|
|
||||||
# If the form was valid
|
# If the form was valid
|
||||||
if form.validate_on_submit():
|
if form.validate_on_submit():
|
||||||
return "Valid URL"
|
return "Valid URL: " + form.url.data
|
||||||
|
|
||||||
return render_template("index.html", form=form)
|
return render_template("index.html", form=form)
|
||||||
|
|||||||
Reference in New Issue
Block a user