How can I stop keypress event in keydown handler?
- What is your exact need?rahul– rahul2009-09-14 13:35:26 +00:00Commented Sep 14, 2009 at 13:35
- whatever type in textbox i need to get the text and should not reflect the typed text into textbox..Santhosh– Santhosh2009-09-14 13:37:12 +00:00Commented Sep 14, 2009 at 13:37
- duplicated stackoverflow.com/questions/1404583/stop-keypress-eventRodrigo– Rodrigo2009-09-14 13:39:51 +00:00Commented Sep 14, 2009 at 13:39
- You asked this before! (I voted to close this.)Wim ten Brink– Wim ten Brink2009-09-14 13:49:54 +00:00Commented Sep 14, 2009 at 13:49
Add a comment |
1 Answer
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; }); 1 Comment
Sudhir Jonathan
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.