The task is as follows: when entering text into input, it is necessary to prohibit certain characters from being entered from the keyboard. tried this:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>func key</title> </head> <body> <input type="text" id="inner"> <p id="outer"></p> </body> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <script> $(function(){ var inner = document.getElementById('inner'); var outer = document.getElementById('outer'); inner.addEventListener("keydown",function(event){ // 79 - > ето буква 'O' if(event.charCode == 79) return false; console.log('hello'); console.log('char code = '+event.charCode); console.log('key code = '+event.keyCode); }) }); </script> </html> 

in this case, I want to prohibit entering the "o" button, returning false, but the event still occurs. Please tell me how I can implement this.

    3 answers 3

     $(function(){ var disabledChars = ['o']; $('#inner').on('keypress', function(e){ var char = String.fromCharCode(e.keyCode); // Введённый символ if(disabledChars.indexOf(char) !== -1) return false; // Если в массиве disabledChars есть введённый символ, то ничего не произойдёт console.info('Char: ' + char); }); }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="inner" autofocus /> 

    • +1). C jQuery met a week ago. Read such answers nice. - Vasil Baymurzin
    • @VasilBaymurzin; I don’t know her at all, I only know a couple of functions :) You're welcome! - user31688
    • Thanks for the native javascript response, helped - Igor Kalamurda
    • @IgorKalamurda; This is not the native JS, but the jQuery library, since it is the code that is used. Although in the native itself, it does not differ. - user31688

    I think if you already connected JQuery, you can do this:

     $(function() { $('#inner').keydown(function(e) { if (e.which == 79) { return false; } }); }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="inner"> <p id="outer"></p> 
    instead

     $(function(){ var inner = document.getElementById('inner'); var outer = document.getElementById('outer'); inner.addEventListener("keydown",function(event){ // 79 - > ето буква 'O' if(event.charCode == 79) return false; console.log('hello'); console.log('char code = '+event.charCode); console.log('key code = '+event.keyCode); }) }); 
    • @romeo, beautifully formatted, thanks). I can not find the time to deal with the formatting of reports. - Vasil Baymurzin
    • Vasil Baymurzin, but not for that :) Everything is simple. - romeo
    • Note that the variant with e.which == 79 does not distinguish between uppercase and lowercase O, and prohibits the input of both. - fori1ton
    • Jay Kveri is a good example, thanks for the reply. - Igor Kalamurda

    return false; in the handler, it works only if the handler was assigned via on :

     inner.onkeydown = function(e) { if (...) return false; }; 

    When adding a handler via addEventListener you need to cancel the event using preventDefault() :

     if (e.charCode === 79) { e.preventDefault(); } 

    http://jsfiddle.net/8m73xsb6/1/

    Pay attention to the code of this plugin for jQuery, it also takes into account pasting text from the clipboard.