I currently have an input box and button on my webpage. When the user types in a number and clicks the button, it is supposed to add the number to a balance I assigned to it. If the user inputs a string, they should receive an error and the balance should not change. Everything seems to work fine, but once the user enters a number+string combination, the program does not work, and continues to add to the balance(for example if the user inputs 23fdsfsd the balance is increasing by 23 and no error message returns. But if the user enters the string fdsfsd the balance goes unchanged and an error message shows up). Here is the code I have as of right now:
document.getElementById('depositButton').addEventListener('click',function(){
let input = parseInt(document.getElementById('userInput').value);
if(input > 0){
initialBalance = initialBalance + input
document.querySelector('#balance span').innerHTML = initialBalance
}else if(input != Number){
document.querySelector('#balance span').innerHTML = initialBalance
document.getElementById('notNumber').innerHTML = "ERROR: Please input a number!"
}
}
)
I also tried getting rid of the parseInt statement in input as well. This didn't work and kept reading anything I typed in as a string.
HTML:
</head>
<body>
<header>
<h1>Welcome to Your ATM:</h1>
<h2>Checking Account:</h2>
</header>
<main>
<section>
<p id="balance">Current Balance: $<span></span></p>
<form>
<label for='amount'>Amount $:</label>
<input id="userInput" type="text" value ="" name = "number" >
<input id = 'resetChecking' type ="reset" value = "Reset">
</form>
<p id = "overdrawn"></p>
<p id = "notNumber"></p>
<div id = "buttons">
<div id = "depositButton" class="button"><button type ="submit">Deposit</button></div>
<br></br>
<div id = "withdrawlButton" class="button"><button type ="submit">Withdrawl</button></div>
</div>