diff --git a/src/app.py b/src/app.py index 8a26824..79f83f5 100644 --- a/src/app.py +++ b/src/app.py @@ -83,7 +83,10 @@ def logout(): def verify_link(link:str) -> bool: if not link.startswith('http://') and not link.startswith('https://'): link = 'http://' + link - reg = r'^(https?:\/\/)(([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,})(:\d+)?(\/[a-zA-Z0-9@:%_\+.~#?&//=]*)?$' + reg = r'^(https?:\/\/)' + \ + r'(([a-zA-Z0-9_-]+\.)+[a-zA-Z]{2,})' + \ + r'(:\d+)?' + \ + r'(\/[a-zA-Z0-9@:%_\+.~#?&//=~-]*)?$' return bool(re.match(reg, link)) diff --git a/src/static/js/validLink.js b/src/static/js/validLink.js index 596758c..a442e62 100644 --- a/src/static/js/validLink.js +++ b/src/static/js/validLink.js @@ -8,24 +8,22 @@ document.addEventListener('DOMContentLoaded', () => { destLinkError.classList.add('error-message'); customLinkError.classList.add('error-message'); - // Append error messages right after inputs destLinkInput.parentNode.insertBefore(destLinkError, destLinkInput.nextSibling); customLinkInput.parentNode.insertBefore(customLinkError, customLinkInput.nextSibling); - // Function to validate URLs const isValidURL = (url) => { const pattern = new RegExp( - '^(https?:\\/\\/)?' + // Protocol (http or https) - '(([a-zA-Z0-9_-]+\\.)+[a-zA-Z]{2,6})' + // Domain name - '(\\/[a-zA-Z0-9@:%_\\+.~#?&//==-]*)?$', // Path, query, or fragment (now includes `=`) + '^(https?:\\/\\/)' + + '(([a-zA-Z0-9_-]+\\.)+[a-zA-Z]{2,})' + + '(\\:\\d+)?' + + '(\\/[a-zA-Z0-9@:%_\\+.~#?&//=~-]*)?$', 'i' - ); - + ); + return pattern.test(url); }; - // Function to add https:// if missing const addHttpsIfMissing = (url) => { if (!/^https?:\/\//i.test(url)) { return 'https://' + url; @@ -33,12 +31,9 @@ document.addEventListener('DOMContentLoaded', () => { return url; }; - // Validate destination link on input destLinkInput.addEventListener('blur', () => { - // Auto-correct the URL if missing protocol destLinkInput.value = addHttpsIfMissing(destLinkInput.value); - // Validate URL if (!isValidURL(destLinkInput.value)) { destLinkError.textContent = 'Please enter a valid URL (e.g., https://example.com)'; } else { @@ -46,7 +41,6 @@ document.addEventListener('DOMContentLoaded', () => { } }); - // Validate custom link for valid characters customLinkInput.addEventListener('input', () => { if (customLinkInput.value && /[^a-zA-Z0-9_-]/.test(customLinkInput.value)) { customLinkError.textContent = 'Custom link can only contain letters, numbers, dashes, and underscores.'; @@ -55,11 +49,9 @@ document.addEventListener('DOMContentLoaded', () => { } }); - // Validate form on submit form.addEventListener('submit', (event) => { let isValid = true; - // Auto-correct the URL if missing protocol destLinkInput.value = addHttpsIfMissing(destLinkInput.value); if (!isValidURL(destLinkInput.value)) { @@ -73,7 +65,7 @@ document.addEventListener('DOMContentLoaded', () => { } if (!isValid) { - event.preventDefault(); // Prevent form submission if validation fails + event.preventDefault(); } }); }); diff --git a/src/templates/form.html b/src/templates/form.html index 583de30..41f2eb4 100644 --- a/src/templates/form.html +++ b/src/templates/form.html @@ -10,7 +10,7 @@