473,624 Members | 2,475 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Mozilla onkeypress issues...

rrocket
116 New Member
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
6 2026
rrocket
116 New Member
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
5,390 Recognized Expert Moderator Expert
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
116 New Member
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
5,390 Recognized Expert Moderator Expert
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
116 New Member
Great, thanks for your help.
Jan 25 '08 #6
gits
5,390 Recognized Expert Moderator Expert
no problem :) post back to the forums anytime you have more questions ...

kind regards
Jan 25 '08 #7

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

Similar topics

7
1937
by: Peter Adolphs | last post by:
Hi newsgroup! The W3C HTML 4.01 recommendation states that "The onreset event occurs when a form is reset." Using Mozilla 1.4.1, how come that if I set onreset="return false;", the form is not reset at all? Is this a bug? I've attached an HTML page to reproduce the behaviour. Bye, Peter.
1
20752
by: Perttu Pulkkinen | last post by:
I have different functions that receive window.event as parameter. Functions are used like this: <input type="text" id="x" onkeypress="return onKeyCurrencyCheck(ev, 'x')" onblur ="onBlurCurrencyCheck(event, 'x')""> Works very well with IE, but window.event and maybe window.event.keycode too seems to be missing form firefox. Is there a workaround for this?
11
31063
by: LilAndy23 | last post by:
How can I use the onKeyPress event handler in Firefox? onKeyPress doesn't seem to fire in Firefox.
4
1530
by: Stefan Mueller | last post by:
Why do I get the following error message in Mozilla if I press a key in the input box? ================================ Error: " nsresult: "0x8057001e (NS_ERROR_XPC_JS_THREW_STRING)" location: "JS frame :: file:///D:/test/index.html :: message :: line 4" data: no] Source File: file:///D:/test/index.html Line: 4
15
21349
by: prabhdeep | last post by:
Hi, Can somebody explain, why in following code, i get "event not defined" error funcTest(sMenu) { doument.getElementById('id1').addEventListener('mousedown', function(){ click(sMenu, event); }, false); }
3
5972
by: Nitinkcv | last post by:
Hi, I have a textbox which takes in values and besides it there is a submit button. Currently on click of the submit button calls a java script function and redirects to a page. So currently after entering the values on hitting the enter on the keyboard does not call the JS Function. So how can i went on to implement this where on hitting enter it calls my JS function: JavaScript Function: function txtKeywordKeyPress() {
3
2466
by: shyamg | last post by:
hi all, This javascript is working IE but not working in FIreFox, validating text fields. var dealerid = new keybEdit('abcdefghijklmnopqurstuvwxyz01234567890 ','Alpha-numeric input only.'); var dealinit = new keybEdit('abcdefghijklmnopqurstuvwxyz01234567890 ','Alpha-numeric input only.'); var dealername = new keybEdit('abcdefghijklmnopqurstuvwxyz ','Alphabets input only.'); var rank = new...
1
2942
by: suneel271 | last post by:
code working in internet explorer but not in mozilla and iam getting erroe like window.event has no properties and here is the code <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <%@taglib uri="/tags/struts-html" prefix="html" %> <%@taglib uri="/tags/struts-bean" prefix="bean" %> <%@taglib uri="/tags/struts-logic"...
3
7020
by: svsenthilkumar | last post by:
hai , i got an error in javascript, when upload a file in a form, the code is working properly in IE but i got javascript error in Mozilla firefos what is mean by error and how to rectify that. the error is Error: uncaught exception: <script language="javascript">
0
8179
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8685
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8341
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7174
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4184
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1489
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.