Santhosh
Santhosh

Reputation: 20426

How to stop keypress event in keydown

How can I stop keypress event in keydown handler?

Upvotes: 3

Views: 9930

Answers (1)

Sudhir Jonathan
Sudhir Jonathan

Reputation: 17516

You can't stop the keypress even from keydown... they're both very separate events. What you can do is cancel the keydown / keypress after reading the character. Here's what I do to allow only letters and numbers to by typed into a 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;                 
          });

Upvotes: 3

Related Questions