Skip to main content
Inspiring
October 27, 2006
Answered

event.keyCode not working in Firefox

  • October 27, 2006
  • 3 replies
  • 1498 views
When i use this validation. Works fine in IE. Not working in firefox.
Gives an error "event not defined".
How to fix this issue in firefox?

<input type="text" name="var_no" onKeyPress="check(this);">

function check(obj){
if(event.keyCode < 45 || event.keyCode > 57)
{
alert('Enter correct value');
obj.focus();
event.keyCode = 0;
}
}
    This topic has been closed for replies.
    Correct answer dave_cozens
    Instead of
    event.keyCode = 0;
    Try
    return false;

    Also, the alert is really annoying from a user perspective. Very intrusive.

    Some other visual cue, such as briefly changing the backgound of the input box or even inserting some text next to it, for example, is friendlier.

    obj.style.background="red";

    3 replies

    dave_cozensCorrect answer
    Inspiring
    October 31, 2006
    Instead of
    event.keyCode = 0;
    Try
    return false;

    Also, the alert is really annoying from a user perspective. Very intrusive.

    Some other visual cue, such as briefly changing the backgound of the input box or even inserting some text next to it, for example, is friendlier.

    obj.style.background="red";
    JavageneAuthor
    Inspiring
    October 31, 2006
    done.
    Inspiring
    October 31, 2006
    Firefox doesn't use event.

    var key = window.event ? e.keyCode : e.which;
    var keychar = String.fromCharCode(key);
    JavageneAuthor
    Inspiring
    October 27, 2006
    dup