I've set input type to "number" and now I want to apply some validations to it. I want to prevent starting the function (after clicking the button) if the input contains "+", "-" or "e".
const countBtn = document.getElementById("forge-points-btn");
function quantityHandler() {
let userInput = document.querySelector(".forge-points-input").value;
if(userInput.split("").includes("e") || userInput.split("").includes("-")) {
return;
}
// else code
}
<input type="number" class="forge-points-input" id="forge-points-input"></input>
<button type="button" id="forge-points-btn">Count</button>
But to my surprise, it doesn't work because when minus, plus or "e" are entered, e.g. "321-51", "+e-", etc. then userInput equals to empty string. However, when "-55" is entered, then userInput displays correct value. Why is that happening? I'm trying to figure out why can't this solution work. I've seen other solutions on this forum, yet I'd like to understand why this one can't work.