3

How can I stop keypress event in keydown handler?

4
  • What is your exact need? Commented 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.. Commented Sep 14, 2009 at 13:37
  • duplicated stackoverflow.com/questions/1404583/stop-keypress-event Commented Sep 14, 2009 at 13:39
  • You asked this before! (I voted to close this.) Commented Sep 14, 2009 at 13:49

1 Answer 1

3

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; }); 
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.