Jump to content


Photo

Firefox is not responding to event.keyCode method


  • Please log in to reply
4 replies to this topic

#1 Fahid

Fahid
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts
  • LocationPakistan

Posted 18 February 2006 - 03:04 AM

HI Guys!

I wanted to make an input box to accept only numeric (int) values, to do that I wrote following function.
function is_int(Key) {
/* 48 is ASCII code for 0 and 57 is for 9 */
    //alert(Key);
    for(i=48;i<=57;i++){
        if(Key == i) return true;
    }
    return false;
}
and my input box look like this
<input type="text" name="quantity" id="quantity" size="30" maxlength="3" value="1" onkeypress="return is_int(event.keyCode);" />

Now! when I use IE6 or Opera, everythings just going perfect, But when I use Firefox, I got a problem.
Firefox make it impossible to type anything in the input box, not even integers.
Infact Firefox return 0, not the actuall ASCII code of the key that was pressed.

[!--sizeo:6--][span style=\"font-size:24pt;line-height:100%\"][!--/sizeo--]Any Idea how to get it right ?[!--sizec--][/span][!--/sizec--]
Back Again...

#2 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 20 February 2006 - 08:19 PM

stumbled upon this line of code
key =
  event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
That should do it.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#3 Fahid

Fahid
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts
  • LocationPakistan

Posted 21 February 2006 - 01:43 AM

Thanks for response ryan, but I still can't find out, how exactly to do it.

this is a sample page which uses the script

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
/*
This funtion helps to make an input-box to only accept integers
*/
function is_int(Key) {
/* 48 is ASCII code for 0 and 57 is for 9 */
    /*
    ryanlwh's code
    Key =  event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    */
    //alert(Key);
    if(Key == 13) return true;
    for(i=48;i<=57;i++){
        if(Key == i) return true;
    }
    return false;
}
</script>
<title>Numeric Only TextBox</title>
</head>

<body>

<input type="text" onkeypress="return is_int(event.keyCode);" />

</body>
</html>

When I use
<input type="text" onkeypress="return is_int(event.keyCode);" />
IE and Opera works just as I want, but Firefox doesn't.

And
When I use
<input type="text" onkeypress="return is_int(event.charCode);" />
Firefox works, but neither IE nor Opera

OK
I changed my javascript to this
function is_int(empty) {
    // in Firefox navigator.appName returns "Netscape".
    if(navigator.appName == "Netscape") Key = event.charCode;
    else Key = event.keyCode;
    alert(Key);
    // removed the rest of code for test
    return false;
}
Still IE and Opera respond well, but not Firefox.
Back Again...

#4 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 22 February 2006 - 07:51 PM

this is what i'm using. i forgot how i googled it but it worked for me
[!--html--][div class=\'htmltop\']HTML[/div][div class=\'htmlmain\'][!--html1--]<input type="text" onkeypress="return is_int(event);" />[!--html2--][/div][!--html3--]
function is_int(event) {
  var Key = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;

  // the following is copied and pasted from yours
  if(Key == 13) return true;
  for(i=48;i<=57;i++){
      if(Key == i) return true;
  }
  return false;
}

may i suggest another way to check the keycode
function is_int(event)
{
  var Key = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
  if(Key==13 || (Key >= 48 && Key <=57)) return true;
  else return false;
}

Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#5 Fahid

Fahid
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts
  • LocationPakistan

Posted 23 February 2006 - 12:17 AM

Thanks a Lot ryanlwh
Now It's working fine.
Thank you very much.
Back Again...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users