i am quite new to JavaScript. I am trying to make it so that if anything other than a number is entered it either blocks it or it takes their entry and tells them to enter a number (because their entry is not acceptable) - I don't know which is easier.
The code I have so far:
const calculateEntered = document.querySelector(".js-
calculateButton ")
const calculateTotal = () => {
let enteredValue = document.querySelector(".js-enterValue").value
if (enteredValue < 40) {
console.log(`<40>`)
enteredValue = parseFloat(enteredValue) + 10
document.querySelector(".js-totalText").innerHTML = `Your
total is: £${enteredValue}`
} else if (enteredValue => 40 && enteredValue != NaN) {
document.querySelector(".js-totalText").innerHTML = `Your total
is: £${enteredValue}`
}
}
<button class="js-calculateButton">Calculate total</button>
<div>
<div>
<p class="js - enterTotalHere">Enter order total here: £</p>
</div>
<div><input placeholder="cost of order" class="js-
enterValue"></input>
</div>
</div>
<p class="js-totalText"> Your total is: </p>
Any characters added lead to the "else if" statement. I tried looking this up but the solutions seemed very difficult - I didn't want to use any 3rd party method.
type="number"?inputelement, you can set it totype="number"and the browser will display a message if the value that's entered is not a number.class="js - enterTotalHere"</input>closing tag doesn't exist. Your code is invalidtype="number"can be used withmin="0"to set a minimum value, zero here as it is a cost, it won't be negative