0

I am attempting to limit which characters may be input into a textbox by capturing the onkeypress event. The following (simplified) snippet works just fine in Google Chrome and Internet Explorer but not Firefox:

function CheckKeyPress(e) {
    alert("Check point #1");
    var x = e || window.event;
    var key = (x.keyCode || x.which);
    alert("Check point #2");
    // do some stuff here
    return
}

Firefox does not trigger the second alert. Why not?

Any and all responses are greatly appreciated.

R Powell
  • 99
  • 1
  • 10
  • Possible duplicate of [jquery keypress event object keyCode for firefox problem?](http://stackoverflow.com/questions/6869996/jquery-keypress-event-object-keycode-for-firefox-problem) – Marcos Pérez Gude Feb 11 '16 at 15:25
  • Are you sure FF is simply not preventing multiple alerts? – Shomz Feb 11 '16 at 15:25
  • How are you calling the event? From inline handler? Any error messages in the console? – Teemu Feb 11 '16 at 15:25
  • use `event.charCode` as the duplicated said – Marcos Pérez Gude Feb 11 '16 at 15:26
  • @MarcosPérezGude Reading the question, an error occurs before the second alert, `keyCode` in the example code won't fire an error. Rather `x` is undefined due to the inline keypress handler. – Teemu Feb 11 '16 at 15:31
  • @Teemu you don't know that. OP only said `Firefox does not trigger the second alert. Why not?`. You don't know if console throws an error or what line is breaking – Marcos Pérez Gude Feb 11 '16 at 15:34
  • @MarcosPérezGude There must be an error, unless OP has prevented multiple alerts from the same page. Anyway, using `charCode` instead of `keyCode` doesn't solve OP's current problem. – Teemu Feb 11 '16 at 15:40
  • @RPowell Checking characters from key codes is not reliable. `keyCode` is just a value for a _key_ on the keyboard, the character it produces might vary depending on localization and even keyboard manufacturer. Use the value of the input and appropriate RexExp instead. – Teemu Feb 11 '16 at 15:54
  • @Teemu, Thanks for the reminder regarding keyboard localization. Shouldn't be a problem with the current app as it is currently targeted for a small US English user community. – R Powell Feb 12 '16 at 16:13
  • I found the following on another post late last night and it appears to have resolved the problem: – R Powell Feb 12 '16 at 16:13
  • I found the following on another post late last night and it appears to have resolved the problem: onkeypress="RunCode(this,arguments[0] || window.event)" While IE accepts the window.event reference, apparently, other browsers accept a more explicit reference passed in the form of an argument. As mentioned, it seems to have resolved the problem. Thanks to all who contributed. – R Powell Feb 12 '16 at 16:20
  • Well, if any other wasn't stated, a public web site is assumed by default when a post is tagged with javascript, html or with a specific browser tag. Anyway, my guess hit, an online handler. You would have saved a lot of search, if you'd answered the questions in the comments. You could still edit your post, it would be more useful to future readers having this same problem. Also, despite of a small audience, I'd still recommend to modify the value with RegExp, it would be more reliable, reusable and simpler way with less code. – Teemu Feb 12 '16 at 16:28

1 Answers1

0

I found an answer while searching another site. The following correction to my inline declaration for the affected textbox resolved my issue:

 onkeypress="CheckKeyPress(this,arguments[0] || window.event)"

It seems Internet Explorer prefers explicit passing of the window.event object while Firefox (and other non-IE browsers) prefer passing an argument reference.

Thanks to all those who contributed a response..

R Powell
  • 99
  • 1
  • 10