4
Good morning. On a system I’m working on, there’s this input inside the first div, and I need to check when the user type the letters I, The, Q in any of the 17 characters of the input, a text appears in the div "Resch" stating that he typed one of these letters, and do not let him continue writing until he deletes these characters.
<div class="col-xs-4 col-md-4">
<input id="txtCh" name="Ch" style="text-transform: uppercase;" onCut="return false" maxlength="17"/>
</div>
<div id="resCh" class="col-sm-4 col-md-4" style="color:red"></div>
The validation is to be done in JS, I’m starting in JS, yet, and I tried to do this function, but did not meet me:
function isValidChar() {
const alert = "INPUT CONTÉM LETRAS PROIBIDAS (I, O, Q)";
const res = document.querySelector("#resCh");
let written = document.querySelector("#txtCh").value;
if (
written.indexOf("o") != -1 ||
written.indexOf("i") != -1 ||
written.indexOf("q") != -1 ||
written.indexOf("O") != -1 ||
written.indexOf("I") != -1 ||
written.indexOf("Q") != -1
) {
document.getElementById("resCh").innerHTML = alert;
document.getElementById("resCh").style.display = "block";
} else {
document.getElementById("resCh").style.display = "none";
}
}
**an Edit: ** I need to monitor the click too, to validate this message... I tried to make a:
document.documentElement.onclick = function(event) {
if (event.target !== res) {
res.style.display = "block";
}
else{
res.style.display = "none";
}
}
but I ended up bugging the message, I don’t know what’s wrong.
Grateful from now on
One thing that can help you check is to use a regular expression. In your
if
you can useif (/[iIoOqQ]/.test(written)) {
.– Benilson