Closed Bug 50275 Opened 25 years ago Closed 25 years ago

JavaScript evt.preventDefault() doesn't cancel onKeyPress event

Categories

(Core :: DOM: UI Events & Focus Handling, defect, P3)

x86
Other
defect

Tracking

()

VERIFIED FIXED

People

(Reporter: dannyg, Assigned: joki)

Details

Attachments

(2 files)

(M17) Invoking evt.preventDefault() in an event listener function for an onKeyPress event does not prevent the typed character from reaching the text input field. Here's a test case for a number filter for a text box: <HTML> <HEAD> <TITLE>Keyboard Capture</TITLE> <SCRIPT LANGUAGE="JavaScript"> function checkIt(evt) { var charCode = evt.charCode status = charCode if (charCode > 31 && (charCode < 48 || charCode > 57)) { alert("Please make sure entries are numbers only.") evt.preventDefault() } } </SCRIPT> </HEAD> <BODY> <FORM> Enter any positive integer: <INPUT TYPE="text" onKeyPress="checkIt(event)"> </FORM> </BODY> </HTML> Expected behavior: For typed numbers, nothing special; for any other character, display alert and prevent erroneous character from reaching the text box. Here is an NN4-compatible version that works as expected in NN4: <HTML> <HEAD> <TITLE>Keyboard Capture</TITLE> <SCRIPT LANGUAGE="JavaScript"> function checkIt(evt) { var charCode = evt.which status = charCode if (charCode > 31 && (charCode < 48 || charCode > 57)) { alert("Please make sure entries are numbers only.") return false } return true } </SCRIPT> </HEAD> <BODY> <FORM> Enter any positive integer: <INPUT TYPE="text" onKeyPress="return checkIt(event)"> </FORM> </BODY> </HTML> Substituting the 'return' scheme for preventDefault() in the moz version doesn't prevent default action, either. This may affect other keyboard events.
confirming for all platforms (note, this bug also occurs on Nav 4.x. IE lets the text be displayed with no error message.) builds: 2000-09-18-08-M18 : Windows 2000-09-18-08-M18 : Mac 2000-09-18-06-M18 : Linux
Status: UNCONFIRMED → NEW
Ever confirmed: true
I somewhat disagree with janc's notes on this bug. From my testing the 'return false' does work correctly in mozilla. The preventDefault() does not work in mozilla but from my tests I'm finding that internally its not ever being called so I'm still piecing that together. As far as the comments on Nav4 and IE every is as expected. Nav4 does the second test but not the first since the DOM standard preventDefault() didn't exist then. IE does neither becase it doesn't support event.which or event.charCode.
Status: NEW → ASSIGNED
Actually I was wrong. Both test cases actually work fine. The problem is that they were copied into the same bug report so the functions were overwriting each other. I'll attach a new test case with this that shows these working. This fix did go in fairly recently so I'll mark this fixed instead of invalid.
Status: ASSIGNED → RESOLVED
Closed: 25 years ago
Resolution: --- → FIXED
Attached file fixed test case
Updating QA Contact.
QA Contact: janc → lorca
Reassigning QA Contact for all open and unverified bugs previously under Lorca's care to Gerardo as per phone conversation this morning.
QA Contact: lorca → gerardok
QA contact updated
QA Contact: gerardok → madhur
verified on build 2001-08-07-08-trunk
Status: RESOLVED → VERIFIED
Component: Event Handling → User events and focus handling
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: