Jump to content

Archived

This topic is now archived and is closed to further replies.

Fahid

Firefox is not responding to event.keyCode method

Recommended Posts

HI Guys!

I wanted to make an input box to accept only numeric (int) values, to do that I wrote following function.
[code]
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;
}
[/code]
and my input box look like this
[code]
<input type="text" name="quantity" id="quantity" size="30" maxlength="3" value="1" onkeypress="return is_int(event.keyCode);" />
[/code]

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 [b]0[/b], 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--]

Share this post


Link to post
Share on other sites
stumbled upon this line of code
[code]key =
  event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;[/code]
That should do it.

Share this post


Link to post
Share on other sites
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

[code]
<!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>
[/code]

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

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

OK
I changed my javascript to this
[code]
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;
}
[/code]
Still IE and Opera respond well, but not Firefox.

Share this post


Link to post
Share on other sites
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--]<[color=blue]input type[/color]="[color=orange]text[/color]" onkeypress="[color=orange]return is_int(event);[/color]" />[!--html2--][/div][!--html3--]
[code]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;
}[/code]

may i suggest another way to check the keycode
[code]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;
}[/code]

Share this post


Link to post
Share on other sites
Thanks a Lot [b]ryanlwh[/b]
Now It's working fine.
Thank you very much.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.