There is an input with an event:

 t.onkeydown = function() { var str = this.value; if(~str.indexOf("123")){ t.value = ""; } } 

Event works only after pressing the second key. Why not after the first?

    2 answers 2

    Processing of the click occurs in several events (there are quite a few of them).
    keydown occurs before inserting a character. That is, when this event occurs, input still looks the same. Either use something later ( keypress , for example), or more adequate means - input .

      Because the symbol is not printed on the keydown event. Catch events: change , input .

      keydown works only at the moment of pressing the key.

      keyup uplift.

      input at input

      change when changing form