1

O código JavaScript abaixo, que serve para "barrar" caracter > (asc 60) só funciona em dispositivos desktop e não em dispositivos mobiles.

<textarea name='notes' id='ta' wrap='physical' cols='65' rows='8' style='width: 80%;' tabindex='14'></textarea>

<script>
var ta = document.getElementById("ta");
ta.addEventListener('keypress',
    function (e) {
       if (e.keyCode == 60) {
           alert('N\u00e3o use <');
           e.preventDefault();
       }
    }
);
</script>
1

2 Respostas 2

-1

beforeinput
InputEvent.data

var ta = document.getElementById('ta');
ta.addEventListener('beforeinput', evt => {
    if (evt.inputType === 'insertText' && evt.data.charCodeAt(0) === 60) {
        alert('N\u00e3o use <');
        evt.preventDefault();
    }
});
<textarea name='notes' id='ta' wrap='hard' cols='65' rows='8' style='width: 80%;' tabindex='14' onpaste='return(false)'></textarea>

var ta = document.getElementById('ta');
ta.addEventListener(window.TextEvent && 'name' in window.TextEvent ? 'textInput' : 'keypress',
    function (evt) {
        if ((evt.which || evt.data.charCodeAt(0)) === 60) {
            alert('N\u00e3o use <');
            evt.preventDefault();
        }
    }, false);
<textarea name='notes' id='ta' wrap='hard' cols='65' rows='8' style='width: 80%;' tabindex='14' onpaste='return(false)'></textarea>

https://www.outsystems.com/blog/posts/create-input-mask-for-mobile/

-2

var ta = document.getElementById("ta");
ta.onkeydown = (e) => {
       const keyCode = e.which || e.keyCode;
       if (keyCode === 188 ) {
           alert('N\u00e3o use <');
           e.preventDefault();
       }
    }
<textarea name='notes' id='ta' wrap='physical' cols='65' rows='8' style='width: 80%;' tabindex='14'></textarea>

O problema está com evento Keypress

Isto porque, certamente há algum problema com o evento keypress no navegador Android (ou seja, Chrome para mobile). Tente substituí-lo pelo evento keydown, isso geralmente funciona bem.

4
  • Não funcionou, nem com desktop nem com mobile
    – Roseli
    21/06/2021 às 13:25
  • Podes por favor tentar agora 21/06/2021 às 13:38
  • @Roseli funcionou? O resto é uma questão de colocar o codigo certo para cada caracter... keycode.info 21/06/2021 às 13:42
  • Olha, agradeço, mas funcionou só no desktop....
    – Roseli
    21/06/2021 às 13:45

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .