473,386 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Key events work in Netscape and IE, but not Firefox - what am I doing wrong

Hi All

I use the following script to stop users typing in anything but standard
chars (eg letters, numbers, etc) into a input textbox:

<!--
// 8 = backspace
// 9 = tab
// 46 = del
// 190 = period (full stop)
// >36 to <41 = 4 x arrow keys
// >47 to <58 = 0 to 9
// >64 to <91 = A to Z
// >95 to <106 = 0 to 9 on numeric keypad

function CheckCodeEntry(e) {
var key = (navigator.appName == "Netscape") ? e.which : e.keyCode;
var shiftcheck = (navigator.appName == "Netscape") ? e.SHIFT_MASK :
event.shiftKey;

if (shiftcheck) {
if (key == 8 || key == 9 ||
key == 46 || (key 36 && key < 41) ||
(key 63 && key < 74) || (key 64 && key < 91) ||
key == 189) {
return true;
}
else {
return false;
}
}

else {
if (key == 8 || key == 9 || key == 190 ||
key == 46 || (key 36 && key < 41) ||
(key 47 && key < 58) || (key 64 && key < 91) ||
(key 95 && key < 106) || key == 189) {
return true;
}
else {
return false;
}
}

}
//-->

The input box is set as follows:

<INPUT TYPE='TEXT' NAME='code' SIZE=30 onKeyDown='return
CheckCodeEntry(event)' onKeydown='return CheckCodeEntry(event)' VALUE="">

Although this appears to work fine in IE and Netscape, Safari and Firefox
only allows me to enter letters. They don't allow me to enter numbers.
Could you please advise as to what I'm doing wrong.

Thanks

Laphan
Nov 8 '06 #1
1 1646
Yobbo wrote:
Hi All

I use the following script to stop users typing in anything but standard
chars (eg letters, numbers, etc) into a input textbox:
[...]
function CheckCodeEntry(e) {
var key = (navigator.appName == "Netscape") ? e.which : e.keyCode;
var shiftcheck = (navigator.appName == "Netscape") ? e.SHIFT_MASK :
event.shiftKey;
[...]
Although this appears to work fine in IE and Netscape, Safari and Firefox
only allows me to enter letters. They don't allow me to enter numbers.
Could you please advise as to what I'm doing wrong.
Firstly, using browser detection rather than feature detection.

Secondly, trying to use key codes to infer the characters entered - how
do you stop pasting of unwanted characters using a context menu?

Thirdly, validating on keystrokes. Use a regular expression to
validate the field when the form is submitted, you don't care what the
user puts in there before then or how they got it there (keystrokes,
pasted, auto-filled). It is much simpler to implement, more reliable
and will work in a greater variety of browsers.

If you want to warn of invalid characters before submission, use
onkeyup or onkeypress, use a RegExp to validate the current content and
write a message to the page warning of invalid input. Let the user fix
it in their own time however they wish.
--
Rob

Nov 9 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mr. T. | last post by:
Hello, i have a page where i have a white box centered on the screen. Now in IE everything shows fine, but in FireFox and Netscape the background of the box isn't white. But all the positioning...
2
by: mortb | last post by:
I create a table containing radiobuttons in client script depending on what choices the user makes. It works fine the radio buttons appear *but* they are *not clickable*. Why? Is there a solution?...
10
by: Brett | last post by:
This code is supposed to work in Netscape 4+ and IE 4+. It works fine in IE but in Netscape 7.2, I get a blank page. Any suggestions? Thanks, Brett <html> <head>
9
by: Lachlan Hunt | last post by:
Hi, I've written some javascript to implement DOM 2 Events in IE (addEventListener, removeEventListener, etc) and I would like to know which browsers it works in. I've tested it in Firefox 1.0.6,...
3
by: niconedz | last post by:
Hi The following code works fine in IE but not Firefox. It's a little script that zooms an image and resizes the window to fit. Can anybody tell me what's wrong? Thanks Nico == btw.....
10
by: News | last post by:
I have a page up trying to learn how to ID a browser and other info. http://wyght.com/warren/testPos.html here is the code <script type = "text/javascript"> var space = ", "; var name...
4
by: John Kotuby | last post by:
Hi all... I am bulding an application and wish to use custom Modal dialog windows. According to a couple of recent articles I have seen, the newer Mozilla browsers (actually I think they said...
2
by: Martin Leese | last post by:
Hi, I am having trouble with CSS in that I can't get the pseudo-element 'first-line' to work. I have some pages that contain dialog. I use a <dlfor this, with the character's name in a...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.