-2
I would like to do an inline validation on an input. I want that if the user type something other than 0 or 1, a message appears warning that only 0’s and 1’s are allowed.
Here’s what I’ve tried:
let valor = document.getElementById("binNumber");
valor.addEventListener('keypress', function(e) {
let conteudo = document.getElementById("binNumber").value
let splited = conteudo.split("")
for (let i = 0; i < valor.length; i++) {
if (splited[i] != 1 || splited[i] != 0) {
alert("erro")
}
}
})
<div>
<label for="binNumber">Binary:</label>
<input id="binNumber">
<label for="decNumber">Decimal</label>
<input id="decNumber"><br>
<button>Convert</button>
</div>
I can’t seem to figure this out. You do not need to give me the solution by hand. I appreciate if you give me a direction. The rest I unroll!
Thanks in advance!
Perfect! Thank you so much! I tested the code, and it worked great. Your comment was super. I didn’t understand everything yet, but it was to delve into what you told me, and try to understand the step-by-step. I will study what you gave me, so I can understand and do in the next! VLW Thiago!
– Felipe
Relax, I’m glad I could help!
– Thiago Sussumu Sato