diff --git a/projects/age-calculator/index.js b/projects/age-calculator/index.js index b2888ee..dcfc148 100644 --- a/projects/age-calculator/index.js +++ b/projects/age-calculator/index.js @@ -4,9 +4,26 @@ const resultEl = document.getElementById("result"); function calculateAge() { const birthdayValue = birthdayEl.value; + + // 🔥 VALIDATION 1: EMPTY CHECK if (birthdayValue === "") { alert("Please enter your birthday"); - } else { + return; + } + + // 🔥 VALIDATION 2: FUTURE DATE BLOCK + const selectedDate = new Date(birthdayValue); + const today = new Date(); + + if (selectedDate > today) { + alert("Future date not allowed"); + return; + } + + // ✅ ORIGINAL CODE + const age = getAge(birthdayValue); + resultEl.innerText = `Your age is ${age} ${age > 1 ? "years" : "year"} old`; +} const age = getAge(birthdayValue); resultEl.innerText = `Your age is ${age} ${age > 1 ? "years" : "year"} old`; }