From d79aefa9a9af02a5413926e97e15012015d18137 Mon Sep 17 00:00:00 2001 From: Lakshmipriyaparthasarathi <168575372+Lakshmipriyaparthasarathi@users.noreply.github.com> Date: Sun, 25 Jan 2026 22:40:41 +0530 Subject: [PATCH] Add input validation for weight and height Added validation to check for non-positive weight and height inputs, ensuring results are hidden if inputs are invalid. --- BMICalculator/phibersoft/app.js | 35 +++++++++++---------------------- 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/BMICalculator/phibersoft/app.js b/BMICalculator/phibersoft/app.js index c9eba4044..8827237f4 100644 --- a/BMICalculator/phibersoft/app.js +++ b/BMICalculator/phibersoft/app.js @@ -1,33 +1,26 @@ -const weightInput = document.getElementById('weight'); -const heightInput = document.getElementById('height'); -const resultElement = document.getElementById('result'); -const bmiElement = document.getElementById('bmi'); -const statusElement = document.getElementById('status'); - -const showResults = () => { - resultElement.style.display = 'block'; -} - -const hideResults = () => { - resultElement.style.display = 'none'; -} - -hideResults(); - const calculateBMI = () => { const weight = weightInput.value; const height = heightInput.value; - if(weight && height){ + + if (weight && height) { + + // ✅ NEW VALIDATION (YOUR CONTRIBUTION) + if (weight <= 0 || height <= 0) { + bmiElement.innerHTML = ''; + statusElement.innerHTML = ''; + hideResults(); + return; + } + const bmi = weight / (height ** 2); - if(isNaN(bmi)) { + if (isNaN(bmi)) { hideResults(); return; } bmiElement.innerText = bmi.toFixed(2); - if (bmi < 18.5) { statusElement.innerText = 'Underweight'; } else if (bmi < 25) { @@ -45,7 +38,3 @@ const calculateBMI = () => { hideResults(); } } - -weightInput.addEventListener('input', calculateBMI); -heightInput.addEventListener('input', calculateBMI); -