From 068e23c45498d146dd1780b0e1dcf7b9de86f797 Mon Sep 17 00:00:00 2001 From: Shivam Kumar Choudhary Date: Wed, 15 Apr 2026 11:54:47 +0530 Subject: [PATCH] Added input validation before API call in currency converter --- projects/currency-converter/index.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/projects/currency-converter/index.js b/projects/currency-converter/index.js index 7514ce1..f1d86e4 100644 --- a/projects/currency-converter/index.js +++ b/projects/currency-converter/index.js @@ -11,6 +11,19 @@ const exchangeRateEl = document.getElementById("exchange-rate"); updateRate() function updateRate() { + + const worth = worthFirstEl.value; + const error = document.getElementById("error"); + if (worth === "" || isNaN(worth) || Number(worth) <= 0) { + error.innerText = "Please enter a valid amount!"; + return; + } else { + error.innerText = ""; + } + + fetch( + `https://v6.exchangerate-api.com/v6/5f9d1c87f7250159c9c9b17d/latest/${currencyFirstEl.value}` + ) fetch( `https://v6.exchangerate-api.com/v6/5f9d1c87f7250159c9c9b17d/latest/${currencyFirstEl.value}` )