Add Validation for time imputs
This commit is contained in:
@@ -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") || "";
|
||||
|
||||
Reference in New Issue
Block a user