From 04624b929867c4522d9ad9958bb460c55c295b18 Mon Sep 17 00:00:00 2001 From: Shivam Kumar Choudhary Date: Fri, 17 Apr 2026 10:01:47 +0530 Subject: [PATCH] Added validation to prevent future date in age calculator ### Changes Made - Added validation for empty input - Prevented future date selection - Improved accuracy of age calculation ### Type - Enhancement --- projects/age-calculator/index.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) 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`; }