proceed if only all URLs are valid

This commit is contained in:
Ryan Mwangi 2024-10-21 19:47:18 +03:00
parent 3ffd34190f
commit ccc8847713
1 changed files with 14 additions and 11 deletions

View File

@ -1,15 +1,15 @@
const form = document.getElementById('merge-form'); const form = document.getElementById('merge-form');
const calendars = document.getElementById('calendars'); const calendars = document.getElementById('calendars');
const addCalendarButton = document.getElementById('add-calendar'); const addCalendarButton = document.getElementById('add-calendar');
const result = document.getElementById('result'); const result = document.getElementById('result');
let alendarIndex = 1; let alendarIndex = 1;
// Function to validate URL format // Function to validate URL format
function isValidUrl(url) { function isValidUrl(url) {
const urlPattern = /^(https?:\/\/[^\s$.?#].[^\s]*)$/; // Regex for URL validation const urlPattern = /^(https?:\/\/[^\s$.?#].[^\s]*)$/; // Regex for URL validation
return urlPattern.test(url); return urlPattern.test(url);
} }
addCalendarButton.addEventListener('click', () => { addCalendarButton.addEventListener('click', () => {
const newCalendar = document.createElement('div'); const newCalendar = document.createElement('div');
@ -24,7 +24,7 @@ const form = document.getElementById('merge-form');
calendarIndex++; calendarIndex++;
}); });
form.addEventListener('submit', (event) => { form.addEventListener('submit', (event) => {
event.preventDefault(); event.preventDefault();
const linkGroupName = document.getElementById('link-group-name').value; const linkGroupName = document.getElementById('link-group-name').value;
const calendarsData = []; const calendarsData = [];
@ -34,6 +34,7 @@ const form = document.getElementById('merge-form');
const prefix = document.getElementById(`prefix-${i}`); const prefix = document.getElementById(`prefix-${i}`);
const override = document.getElementById(`override-${i}`); const override = document.getElementById(`override-${i}`);
const url = document.getElementById(`url-${i}`); const url = document.getElementById(`url-${i}`);
if (prefix && override && url) { if (prefix && override && url) {
// Validate the URL // Validate the URL
if (!isValidUrl(url.value)) { if (!isValidUrl(url.value)) {
@ -48,6 +49,7 @@ const form = document.getElementById('merge-form');
} }
} }
} }
if (valid) {
fetch('/merge', { fetch('/merge', {
method: 'POST', method: 'POST',
headers: { headers: {
@ -69,4 +71,5 @@ const form = document.getElementById('merge-form');
console.error('Error:', error); console.error('Error:', error);
result.innerHTML = `Error merging calendars: ${error.message || 'Unknown error'}` result.innerHTML = `Error merging calendars: ${error.message || 'Unknown error'}`
}); });
}); }
});