This commit is contained in:
@@ -2,21 +2,19 @@ export function validateAgeLive(inputEl, minAge = 5, maxAge = 18) {
|
||||
const dob = inputEl.value.trim();
|
||||
if (!dob) return false;
|
||||
|
||||
const birthDate = normalize(new Date(dob));
|
||||
const birthDate = parseDateOnly(dob);
|
||||
if (isNaN(birthDate.getTime())) {
|
||||
showFeedback(inputEl, false, "Invalid date format (Use YYYY-MM-DD)");
|
||||
return false;
|
||||
}
|
||||
|
||||
const registrationAgeDeadline = window.appConfig?.registrationAgeDeadline;
|
||||
const deadline = normalize(new Date(registrationAgeDeadline || new Date()));
|
||||
const schoolYearAgeDeadline = window.appConfig?.schoolYearAgeDeadline || registrationAgeDeadline;
|
||||
const minimumAgeDeadline = parseDateOnlyOrToday(registrationAgeDeadline);
|
||||
const ageDeadline = parseDateOnlyOrToday(schoolYearAgeDeadline);
|
||||
|
||||
// Calculate boundaries - add one day to effectively make the age calculation more lenient
|
||||
const adjustedDeadline = new Date(deadline);
|
||||
adjustedDeadline.setDate(adjustedDeadline.getDate() + 1);
|
||||
|
||||
const minBirthDate = new Date(Date.UTC(adjustedDeadline.getFullYear() - maxAge, adjustedDeadline.getMonth(), adjustedDeadline.getDate())); // oldest
|
||||
const maxBirthDate = new Date(Date.UTC(adjustedDeadline.getFullYear() - minAge, adjustedDeadline.getMonth(), adjustedDeadline.getDate())); // youngest
|
||||
const minBirthDate = new Date(Date.UTC(ageDeadline.getUTCFullYear() - maxAge - 1, ageDeadline.getUTCMonth(), ageDeadline.getUTCDate() + 1)); // oldest
|
||||
const maxBirthDate = new Date(Date.UTC(minimumAgeDeadline.getUTCFullYear() - minAge, minimumAgeDeadline.getUTCMonth(), minimumAgeDeadline.getUTCDate())); // youngest
|
||||
|
||||
const isValidAge = birthDate >= minBirthDate && birthDate <= maxBirthDate;
|
||||
|
||||
@@ -34,7 +32,7 @@ export function validateAgeLive(inputEl, minAge = 5, maxAge = 18) {
|
||||
|
||||
const errorMessage = isValidAge
|
||||
? ""
|
||||
: `Must be ${minAge}-${maxAge} years old by ${formatDateMMDDYYYY(registrationAgeDeadline)}.`;
|
||||
: `Must be at least ${minAge} years old by ${formatDateMMDDYYYY(registrationAgeDeadline)} and no older than ${maxAge} by ${formatDateMMDDYYYY(schoolYearAgeDeadline)}.`;
|
||||
|
||||
showFeedback(inputEl, isValidAge, errorMessage);
|
||||
return isValidAge;
|
||||
@@ -44,6 +42,31 @@ function normalize(date) {
|
||||
return new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
|
||||
}
|
||||
|
||||
function parseDateOnly(value) {
|
||||
const match = String(value || '').match(/^(\d{4})-(\d{2})-(\d{2})$/);
|
||||
if (!match) return new Date(NaN);
|
||||
|
||||
const year = Number(match[1]);
|
||||
const monthIndex = Number(match[2]) - 1;
|
||||
const day = Number(match[3]);
|
||||
const date = new Date(Date.UTC(year, monthIndex, day));
|
||||
|
||||
if (
|
||||
date.getUTCFullYear() !== year ||
|
||||
date.getUTCMonth() !== monthIndex ||
|
||||
date.getUTCDate() !== day
|
||||
) {
|
||||
return new Date(NaN);
|
||||
}
|
||||
|
||||
return date;
|
||||
}
|
||||
|
||||
function parseDateOnlyOrToday(value) {
|
||||
const date = parseDateOnly(value);
|
||||
return isNaN(date.getTime()) ? normalize(new Date()) : date;
|
||||
}
|
||||
|
||||
|
||||
// Helper function (unchanged)
|
||||
function showFeedback(inputEl, isValid, message) {
|
||||
@@ -57,17 +80,17 @@ function showFeedback(inputEl, isValid, message) {
|
||||
// Set max birthdate (2025 - minAge)
|
||||
window.addEventListener("DOMContentLoaded", () => {
|
||||
const minAge = 5; // Must match validateAgeLive's default
|
||||
const deadline = window.appConfig?.registrationAgeDeadline
|
||||
? new Date(window.appConfig.registrationAgeDeadline)
|
||||
: new Date();
|
||||
const maxBirthYear = deadline.getFullYear() - minAge;
|
||||
const maxDateStr = `${maxBirthYear}-12-31`; // Latest allowed birthdate
|
||||
const deadline = parseDateOnlyOrToday(window.appConfig?.schoolYearAgeDeadline);
|
||||
const registrationAgeDeadline = parseDateOnly(window.appConfig?.registrationAgeDeadline);
|
||||
const maxDeadline = isNaN(registrationAgeDeadline.getTime()) ? deadline : registrationAgeDeadline;
|
||||
const maxBirthYear = maxDeadline.getUTCFullYear() - minAge;
|
||||
const maxDateStr = `${maxBirthYear}-12-31`; // Latest allowed birthdate for age-5 registration grace
|
||||
|
||||
document.querySelectorAll(".dob-input").forEach((input) => {
|
||||
input.setAttribute("max", maxDateStr);
|
||||
input.setAttribute(
|
||||
"aria-label",
|
||||
`Born before ${maxBirthYear + 1} (${minAge}+ years by ${deadline.getFullYear()})`
|
||||
`Born before ${maxBirthYear + 1} (${minAge}+ years by ${maxDeadline.getUTCFullYear()})`
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user