By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,630 Members | 1,126 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,630 IT Pros & Developers. It's quick & easy.

Mozilla onkeypress issues...

rrocket
100+
P: 116
Anyone know how to get onkeypress to work with keycode? I need to figure out what the keycode is before the character shows up in a textbox. Here is the code I have so far (see below). I can get the value to show with onkeydown or up, but it shows the typed value in the textbox even if it does not fit my criteria.

Expand|Select|Wrap|Line Numbers
  1. //Call it with onkeypress = return stripper(event)
  2. function stripper(e)
  3. {
  4.     var bRes = false;
  5.     if((e.keyCode >= 48)&&(e.keyCode<=57))
  6.     {
  7.         alert(e.keyCode);
  8.         bRes=true;
  9.     }
  10.     else
  11.     {
  12.         alert("Numeric Values Only");
  13.  
  14.     }
  15.     return bRes;
  16. }
  17.  
Basically what this is supposed to do is not show the typed character if it is anything but a number. With onkeypress Mozilla returns a 0 for the keyCode... No issues with IE.
Jan 24 '08 #1
Share this Question
Share on Google+
6 Replies


rrocket
100+
P: 116
Found the answer on another post... Sort of anyway.
http://www.thescripts.com/forum/thread89237.html

Here is the code just to make things easier:
Expand|Select|Wrap|Line Numbers
  1. //call it with onkeypress="return stripper(event)"
  2. function stripper(e)
  3. {
  4.     var bRes = false;
  5.     if(e.keyCode > 0)
  6.     {
  7.         if((e.keyCode >= 48)&&(e.keyCode <= 57))
  8.         {
  9.             bRes=true
  10.         }
  11.         else
  12.         {
  13.             alert("Numeric values only");
  14.         }
  15.     }
  16.     else
  17.     {
  18.         if((e.charCode >= 48)&&(e.charCode<=57))
  19.         {
  20.             //alert(e.charCode);
  21.             bRes=true;
  22.         }
  23.         else
  24.         {
  25.             alert("Numeric Values Only");
  26.             //alert(e.charCode);
  27.         }
  28.     }
  29.     return bRes;
  30. }
  31.  
keyCode works fine in IE and charCode works for Mozilla... keyCode comes out as 0 in Mozilla so I check for that first as sort of a browser check and everyone seems happy at the moment.
Jan 24 '08 #2

gits
Expert Mod 5K+
P: 5,333
hi ...

glad to hear you got it working ... just a note in case you want to write it a little bit shorter :)

Expand|Select|Wrap|Line Numbers
  1. function stripper(e) {
  2.     var bRes = false;
  3.     var func = { 'true': 'keyCode', 'false': 'charCode' };
  4.     var val  = (e.keyCode > 0).toString();
  5.  
  6.     if (e[func[val]] >= 48 && e[func[val]] <= 57) {
  7.         bRes=true
  8.     } else {
  9.         alert("Numeric values only");
  10.     }
  11.  
  12.     return bRes;
  13. }
  14.  
kind regards
Jan 24 '08 #3

rrocket
100+
P: 116
Cool thanks. Your version looks less like a hack, LOL. I am not familiar with what you actually did... What would I search for to get more information on that method?
Jan 25 '08 #4

gits
Expert Mod 5K+
P: 5,333
method? :) ... it simply uses a kind of dynamic function calls that would depend on the condition. the 'magic' is to use an object to store the function-names. the basics are:

Expand|Select|Wrap|Line Numbers
  1. var obj = {
  2.     foo: function() { alert('foo'); },
  3.     bar: function() { alert('bar'); }
  4. };
  5.  
  6. // we may refer with:
  7. obj.foo();
  8.  
  9. // or even:
  10. obj['foo']();
  11.  
kind regards
Jan 25 '08 #5

rrocket
100+
P: 116
Great, thanks for your help.
Jan 25 '08 #6

gits
Expert Mod 5K+
P: 5,333
no problem :) post back to the forums anytime you have more questions ...

kind regards
Jan 25 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.