Fixed validation?

This commit is contained in:
Boyan 2024-12-08 00:28:07 +01:00
parent b7cd6c331a
commit 998f7ae2e3
3 changed files with 12 additions and 17 deletions

View File

@ -83,7 +83,10 @@ def logout():
def verify_link(link:str) -> bool: def verify_link(link:str) -> bool:
if not link.startswith('http://') and not link.startswith('https://'): if not link.startswith('http://') and not link.startswith('https://'):
link = 'http://' + link 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)) return bool(re.match(reg, link))

View File

@ -8,16 +8,15 @@ document.addEventListener('DOMContentLoaded', () => {
destLinkError.classList.add('error-message'); destLinkError.classList.add('error-message');
customLinkError.classList.add('error-message'); customLinkError.classList.add('error-message');
// Append error messages right after inputs
destLinkInput.parentNode.insertBefore(destLinkError, destLinkInput.nextSibling); destLinkInput.parentNode.insertBefore(destLinkError, destLinkInput.nextSibling);
customLinkInput.parentNode.insertBefore(customLinkError, customLinkInput.nextSibling); customLinkInput.parentNode.insertBefore(customLinkError, customLinkInput.nextSibling);
// Function to validate URLs
const isValidURL = (url) => { const isValidURL = (url) => {
const pattern = new RegExp( const pattern = new RegExp(
'^(https?:\\/\\/)?' + // Protocol (http or https) '^(https?:\\/\\/)' +
'(([a-zA-Z0-9_-]+\\.)+[a-zA-Z]{2,6})' + // Domain name '(([a-zA-Z0-9_-]+\\.)+[a-zA-Z]{2,})' +
'(\\/[a-zA-Z0-9@:%_\\+.~#?&//==-]*)?$', // Path, query, or fragment (now includes `=`) '(\\:\\d+)?' +
'(\\/[a-zA-Z0-9@:%_\\+.~#?&//=~-]*)?$',
'i' 'i'
); );
@ -25,7 +24,6 @@ document.addEventListener('DOMContentLoaded', () => {
}; };
// Function to add https:// if missing
const addHttpsIfMissing = (url) => { const addHttpsIfMissing = (url) => {
if (!/^https?:\/\//i.test(url)) { if (!/^https?:\/\//i.test(url)) {
return 'https://' + url; return 'https://' + url;
@ -33,12 +31,9 @@ document.addEventListener('DOMContentLoaded', () => {
return url; return url;
}; };
// Validate destination link on input
destLinkInput.addEventListener('blur', () => { destLinkInput.addEventListener('blur', () => {
// Auto-correct the URL if missing protocol
destLinkInput.value = addHttpsIfMissing(destLinkInput.value); destLinkInput.value = addHttpsIfMissing(destLinkInput.value);
// Validate URL
if (!isValidURL(destLinkInput.value)) { if (!isValidURL(destLinkInput.value)) {
destLinkError.textContent = 'Please enter a valid URL (e.g., https://example.com)'; destLinkError.textContent = 'Please enter a valid URL (e.g., https://example.com)';
} else { } else {
@ -46,7 +41,6 @@ document.addEventListener('DOMContentLoaded', () => {
} }
}); });
// Validate custom link for valid characters
customLinkInput.addEventListener('input', () => { customLinkInput.addEventListener('input', () => {
if (customLinkInput.value && /[^a-zA-Z0-9_-]/.test(customLinkInput.value)) { if (customLinkInput.value && /[^a-zA-Z0-9_-]/.test(customLinkInput.value)) {
customLinkError.textContent = 'Custom link can only contain letters, numbers, dashes, and underscores.'; 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) => { form.addEventListener('submit', (event) => {
let isValid = true; let isValid = true;
// Auto-correct the URL if missing protocol
destLinkInput.value = addHttpsIfMissing(destLinkInput.value); destLinkInput.value = addHttpsIfMissing(destLinkInput.value);
if (!isValidURL(destLinkInput.value)) { if (!isValidURL(destLinkInput.value)) {
@ -73,7 +65,7 @@ document.addEventListener('DOMContentLoaded', () => {
} }
if (!isValid) { if (!isValid) {
event.preventDefault(); // Prevent form submission if validation fails event.preventDefault();
} }
}); });
}); });

View File

@ -10,7 +10,7 @@
<h1>Create a Redirect</h1> <h1>Create a Redirect</h1>
<form method="post"> <form method="post">
<input type="text" name="dest_link" placeholder="https://example.com" required><br> <input type="text" name="dest_link" placeholder="https://example.com" required><br>
<input type="text" name="custom_link" placeholder="https://example2.com"><br> <input type="text" name="custom_link" placeholder="cool little name"><br>
<button type="submit">Create Redirect</button> <button type="submit">Create Redirect</button>
</form> </form>
{% with messages = get_flashed_messages() %} {% with messages = get_flashed_messages() %}