From 8a36333883d2dab345e3a982131e30ad852955cc Mon Sep 17 00:00:00 2001 From: luxick Date: Fri, 3 Apr 2026 15:27:26 +0200 Subject: [PATCH] Add Validation for time imputs --- js/event-popup.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/js/event-popup.js b/js/event-popup.js index eb893a8..b019198 100644 --- a/js/event-popup.js +++ b/js/event-popup.js @@ -458,6 +458,39 @@ }); } + function setupTimeInputNormalization(input) { + // Track digits typed into the hours sub-field so we can reconstruct + // "HH:00" when the user fills hours but leaves minutes as "--". + var typedDigits = ""; + + input.addEventListener("keydown", function (e) { + if (this.value) { + // Field already has a valid complete value — reset tracking. + typedDigits = ""; + return; + } + if (/^\d$/.test(e.key)) { + typedDigits = (typedDigits + e.key).slice(-2); + } else if (e.key === "Backspace" || e.key === "Delete") { + typedDigits = ""; + } + }); + + input.addEventListener("change", function () { + typedDigits = ""; + }); + + input.addEventListener("blur", function () { + if (!this.value && this.validity && this.validity.badInput) { + var h = parseInt(typedDigits, 10); + if (!isNaN(h) && h >= 0 && h <= 23) { + this.value = pad2(h) + ":00"; + } + typedDigits = ""; + } + }); + } + function renderForm(data, slots) { var isEdit = data.mode === "edit"; var title = isEdit ? "Edit Event" : "Create Event"; @@ -565,6 +598,12 @@ timeFields.style.display = allDayCheckbox.checked ? "none" : ""; }); } + + // Wire up time normalization: auto-substitute :00 for missing minutes + var startTimeInput = popup.querySelector(".luxtools-form-start-time"); + var endTimeInput = popup.querySelector(".luxtools-form-end-time"); + if (startTimeInput) setupTimeInputNormalization(startTimeInput); + if (endTimeInput) setupTimeInputNormalization(endTimeInput); } function collectFormData() { @@ -600,6 +639,22 @@ return; } + if (!formData.allDay) { + var popup = getDialog().getContainer(); + var startInput = popup.querySelector(".luxtools-form-start-time"); + var endInput = popup.querySelector(".luxtools-form-end-time"); + if ( + (startInput && startInput.validity && startInput.validity.badInput) || + (endInput && endInput.validity && endInput.validity.badInput) + ) { + showNotification( + "Please complete the time fields or clear them.", + "error", + ); + return; + } + } + // For recurring event edits, ask about scope first if (mode === "edit") { var recurrence = saveBtn.getAttribute("data-recurrence") || "";