timepicker-ui
4.6.0GitHub
timepicker-ui
Constraints / Validation

Validation

Mark a date invalid with your own message using calendar.validate in @pglejzer/datepicker-ui.

Weekdays only

#

calendar.validate runs on the presented value (null when empty) after setValue(), typed input and every commit. Return a message to mark the input invalid, or null, undefined or an empty string when the value is fine. It never blocks a pick or a commit.

A message sets aria-invalid on the input and emits an error event with the code invalid-value. The demo shows it under the field, wired to the input with aria-describedby. Pick a Saturday or a Sunday and confirm to see it.

Weekdays only
import DatepickerUI from "@pglejzer/datepicker-ui";
import "@pglejzer/datepicker-ui/index.css";
const message = document.querySelector("#date-message");
new DatepickerUI("#date", {
calendar: {
validate: (value) => {
if (value === null || !("date" in value)) return null;
const day = value.date.getDay();
return day === 0 || day === 6 ? "Pick a weekday" : null;
},
},
callbacks: {
onError: ({ message: text }) => {
message.textContent = text;
},
onConfirm: ({ value }) => {
message.textContent = value && "date" in value && [0, 6].includes(value.date.getDay())
? "Pick a weekday"
: "";
},
},
}).create();

Typed dates

#

Typed text that is not a date, or a date outside minDate and maxDate, emits error with the code invalid-date or unavailable-date. The default messages are labels.invalidDate and labels.unavailableDate. The same onError callback receives them, so the message above covers those cases too.

Docs: Typed Input · Events · Options

Esc