Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My javascript code not working in mozilla firefox

This is my code for making textbox accept number only.

<asp:TextBox ID="txtRotationNo" runat="server" onkeydown="return NumberOnly();" CssClass="textbox"></asp:TextBox>

function NumberOnly () {
    if(!(event.keyCode>=48 && event.keyCode<=57) && event.keyCode!=8) {
        event.returnValue=null;
    }
}

This code is working in Chrome and Opera, but not in firefox.

Can you tell me what's wrong with this code?

like image 690
Vibin Jith Avatar asked Feb 27 '23 23:02

Vibin Jith


1 Answers

Many things wrong with the code, including the lack of an event argument, plus the wrong way about cancelling the event. Here, just replace the code with this:

function NumberOnly(e) {
  e = e || window.event; // remove this if you don't need IE support
  if (!(e.keyCode >= 48 && e.keyCode <= 57) && e.keyCode != 8)
    e.preventDefault();  // standard method of cancelling event
  return false;          // IE method of cancelling event
}
like image 163
Delan Azabani Avatar answered Mar 07 '23 22:03

Delan Azabani