How to stop a keypress event in keydown

how can i stop the keypress event in the keydown handler.

+3
source share
1 answer

You cannot stop a keystroke even from keydown ... they are both separate events. What you can do is cancel keydown / keypress after reading the character. Here I am making sure that only letters and numbers are entered in the text box (jQuery)

  urlBox.keypress(function(e){
            if(e.which == 13 || e.which == 8 || e.which == 0) return true;
            if(48 <= e.which && e.which <= 57) return true;
            if(65 <= e.which && e.which <= 90) return true;
            if(97 <= e.which && e.which <= 122) return true;
            return false;                 
          });
+3
source

Source: https://habr.com/ru/post/1717649/


All Articles