Jump to content

Firefox is not responding to event.keyCode method


Fahid

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--]
Link to comment
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.
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.