469,353 Members | 2,232 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,353 developers. It's quick & easy.

Javascript Keys routine doesn't work in FireFox

Hi All

I have a JS routine to prohibit users from entering a range of chars in a
textbox and although this works perfectly in IE x, in the latest version of
Firefox it appears to be blocking keys that I want to allow. The routine
sort of still works, but its as if the mapping of the keys is worked out
completely differently. I thought the key maps would be universal.

My code is as follows:

HTML:

<INPUT TYPE="TEXT" NAME="qty" SIZE=5 MAXLENGTH=12 CLASS='FormQtyBox'
VALUE="1" onKeyDown='return CheckQtyEntry(event)' onKeydown='return
CheckQtyEntry(event)'>

JS:

function CheckQtyEntry(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 > 95 && key < 106)) {
return true;
}
else {
return false;
}
}
else {
if (key == 8 || key == 9 || key == 46 || (key > 36 && key < 41) ||
(key > 47 && key < 58) || (key > 95 && key < 106)) {
return true;
}
else {
return false;
}
}
}

This routine basically forces the user to only enter numbers from either the
main keyboard set or the numeric keypad. Like I said before it works fine
in IE, but the key codes for Firefox seem to be different.

Any ideas as to what the solution is?

Thanks

Robbie

Feb 7 '06 #1
2 2891
Sorted it.

Found a great tutorial in Google - sorry for the hassle!!

Rgds Robbie
"Astra" <No@Spam.com> wrote in message
news:11************@corp.supernews.com...
Hi All

I have a JS routine to prohibit users from entering a range of chars in a
textbox and although this works perfectly in IE x, in the latest version of
Firefox it appears to be blocking keys that I want to allow. The routine
sort of still works, but its as if the mapping of the keys is worked out
completely differently. I thought the key maps would be universal.

My code is as follows:

HTML:

<INPUT TYPE="TEXT" NAME="qty" SIZE=5 MAXLENGTH=12 CLASS='FormQtyBox'
VALUE="1" onKeyDown='return CheckQtyEntry(event)' onKeydown='return
CheckQtyEntry(event)'>

JS:

function CheckQtyEntry(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 > 95 && key < 106)) {
return true;
}
else {
return false;
}
}
else {
if (key == 8 || key == 9 || key == 46 || (key > 36 && key < 41) ||
(key > 47 && key < 58) || (key > 95 && key < 106)) {
return true;
}
else {
return false;
}
}
}

This routine basically forces the user to only enter numbers from either the
main keyboard set or the numeric keypad. Like I said before it works fine
in IE, but the key codes for Firefox seem to be different.

Any ideas as to what the solution is?

Thanks

Robbie


Feb 7 '06 #2
Astra wrote:
Sorted it.

Found a great tutorial in Google - sorry for the hassle!!
URL?
[...]
"Astra" <No@Spam.com> wrote in message
news:11************@corp.supernews.com...
Hi All

I have a JS routine to prohibit users from entering a range of chars in a
textbox and although this works perfectly in IE x, in the latest version of
Firefox it appears to be blocking keys that I want to allow. The routine
sort of still works, but its as if the mapping of the keys is worked out
completely differently. I thought the key maps would be universal.
If you want to restrict input to certain characters, forget using keycodes
and look at the actual text that has been entered. Use a regular
expression to check it fits your requirements.

Methods based on keycodes are easily defeated and very unreliable.


My code is as follows:

HTML:

<INPUT TYPE="TEXT" NAME="qty" SIZE=5 MAXLENGTH=12 CLASS='FormQtyBox'
VALUE="1" onKeyDown='return CheckQtyEntry(event)' onKeydown='return
CheckQtyEntry(event)'>

JS:

function CheckQtyEntry(e) {
var key = (navigator.appName == "Netscape") ? e.which : e.keyCode;
Browser sniffing is soooo last millennium.

var shiftcheck = (navigator.appName == "Netscape") ? e.SHIFT_MASK :
event.shiftKey;
And again...
[...]

This routine basically forces the user to only enter numbers from either the
main keyboard set or the numeric keypad.
No, it doesn't. If JS is disabled the user can press any key they like
with impunity. Or use copy/paste. Or use script entered via the address
bar to set the value, or...

Like I said before it works fine
in IE, but the key codes for Firefox seem to be different.
That is another reason why keycodes are very unreliable.


Any ideas as to what the solution is?


Use a regular expression or other method to check the characters entered,
not the keys pressed.
--
Rob
Feb 7 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by tnhoe | last post: by
9 posts views Thread by Astra | last post: by
7 posts views Thread by C.Joseph Drayton | last post: by
41 posts views Thread by Rene Nyffenegger | last post: by
8 posts views Thread by Mateusz Viste | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.