473,785 Members | 2,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.appN ame == "Netscape") ? e.which : e.keyCode;
var shiftcheck = (navigator.appN ame == "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='retu rn
CheckCodeEntry( event)' onKeydown='retu rn 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 1664
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.appN ame == "Netscape") ? e.which : e.keyCode;
var shiftcheck = (navigator.appN ame == "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
6005
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 is more or less OK. This is the code of my page: <div class="centraal"> <div class="whitebox">
2
2095
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? I'm using IE 6.0 cheers, mortb actual code included (cut from a larger page):
10
1570
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
1665
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, Deer Park Alpha 2, Opera 8.02 and IE6 and IE7 Beta 1, all on WinXP SP2. Could some of you please take a look at the test pages in any browser you have available to you, and let me know if it works or if you encounter any problems? If...
3
2957
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.. sorry for the long post ==
10
2376
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 = navigator.appName;
4
10636
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 Netscape) supported syntax like : window.open(URL ,"diagwin","modal=1, width=30"); For IE I use : window.showModalDialog
2
2439
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 <dtfollowed by the words in a <dd>. This is rendered as I wish with the first line not indented, the following lines indented, and no
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10327
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...
0
10151
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
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
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2879
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.