views:

471

answers:

1

how can i stop keypress event in keydown handler.

+1  A: 

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;                 
          });
Sudhir Jonathan
return true false is only in IE not in firefox.... any idea.
santose
I don't think its browser dependent. Anyway, you could also call prevent default if you want to be safe. Look up the docs for that.
Sudhir Jonathan

related questions