[n00b] Input Field Character Filtering: Opera Problem
Ladies and Gentlemen, hello. This is my first post here ever.
The following two simple examples are to perform the same task - block numeric input in text field, except one of the works in Opera and the other one doesn't:
Example from W3 Schools - DOES NOT WORK!
Example from MR ED KJ - WORKS!
if(window.event) // IE
keynum = e.keyCode;
else if(e.which) // Netscape/Firefox/Opera
keynum = e.which;
keychar = String.fromCharCode(keynum);
numcheck = /\d/;
<input type="text" onkeydown="return noNumbers(event)" />
The scripts were tested under Windows XP Pro SP3 using the following browsers:
var key = window.event ? e.keyCode : e.which;
var keychar = String.fromCharCode(key);
reg = /\d/;
<input type="text" onkeypress="return onKeyPressBlockNumbers(event);" />
- Chrome: 126.96.36.199
- IE: 8.0.6001. 18702IC
- Firefox: 3.0.13
- Flock: 2.0.3
- Opera: 9.63
- Safari 3.2.1 (525.27.1)
Any ideas what the reason may be? There's good chance this problem will be popping up frequently in the future, so it would really help to know what makes those things tick.
Thank you for your time.
Usually this kind of problem (numeric or alphanumeric validation in form's controls) is to be solved otherwise, on using onkeyup (and oblur) events and Regular Expressions:
onkeydown and onkeypress are tricky events. Different browsers treat them in different ways, thus they are to be avoided in validation codes.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<input type="text" onkeyup="noNumbers(this)" onblur="noNumbers(this)">
Usual RegExp validation codes:
Last edited by Kor; 08-31-2009 at 04:33 AM.
Thank you for the response and the links, Kor.
Yes! As it turned out I did neglect the main trigger event difference, which only came to my attention a couple of hours after posting. I can see from the links that regular expressions are even bigger than I imagined.
How right you are, but I really wanted it in the project for purely aesthetic reasons. My practical goal was actually the exact opposite – allow number only. I've kinda managed to do that (works is Opera too) but at this point I don't know how to bring back the text editing commands that require key combinations e.g. Copy, Paste, Select All, etc. Furthermore, it's not as elegant as it could be.
Originally Posted by You
That is exactly one of the reasons for you should use RegExp
Originally Posted by Illusion Sector
Haha! Got it!
All seems to work fine now.
Would regExp really help reduce code in this particular case though?
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread