By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,480 Members | 1,743 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,480 IT Pros & Developers. It's quick & easy.

Firefox event.keyCode problem (help......)

P: n/a
Help....

I've got a textarea where people type in a description. However for
certain reasons we need to stop them typing !$*^ .

I have a solution this which works fine in IE:

function keypress()
{

if((event.keyCode==33)||(event.keyCode==36)||(even t.keyCode==42)||(event.keyCode==94))
{
//bad key pressed
event.returnValue = false;
}
}

This function is in a javascript file and called from the textarea like
this:

<textarea rows="13" maxLength="860" cols="30" name="remarks"
id="remarks" title="description"
onchange="keypress();" onKeypress="keypress();" onKeyDown="keypress();"
onKeyUp="keypress();"
value =""></textarea>

However, it doesn't work in Firefox. I changed event.keyCode to
event.which . However, this only works if the javascript is embedded in
the html and not in my .js file. However, one of the requirements for
my project is that it is W3c compliant and therefore the javascript
should be in separate files and not embedded in the html.

Does anyone know how I can successfully get the event object in a
javascript file using firefox so I can user event.which ???

Cheers...Rich

Sep 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
ri************@talk21.com wrote:
Help....

I've got a textarea where people type in a description. However for
certain reasons we need to stop them typing !$*^ .

I have a solution this which works fine in IE:

function keypress()
{
function keypress(event)
{
event = event || window.event; // window.event for IE
...
<textarea rows="13" maxLength="860" cols="30" name="remarks"
id="remarks" title="description"
onchange="keypress();" onKeypress="keypress();" onKeyDown="keypress();"
onKeyUp="keypress();"
value =""></textarea>
onchange="keypress(event);" onKeypress="keypress(event);"
onKeyDown="keypress(event);"

However, this only works if the javascript is embedded in
the html and not in my .js file. However, one of the requirements for
my project is that it is W3c compliant and therefore the javascript
should be in separate files and not embedded in the html.
This part did not make any sense to me.
Does anyone know how I can successfully get the event object in a
javascript file using firefox so I can user event.which ???


No need to use event.which. Just use event.keyCode according to W3
standards.
Sep 22 '05 #2

P: n/a
On 22/09/2005 14:11, Robert wrote:
ri************@talk21.com wrote:


[snip]
function keypress()
{


function keypress(event)
{
event = event || window.event; // window.event for IE


This construct is only necessary when a function object will be assigned
directly to a event property. That is:

myElement.onkeypress = keypress;

In a later suggestion (omitted) you pass the event object to the
function so the above is unnecessary.

[snip]
However, this only works if the javascript is embedded in
the html and not in my .js file. However, one of the requirements for
my project is that it is W3c compliant and therefore the javascript
should be in separate files and not embedded in the html.
Using intrinsic event attributes doesn't violate any particular W3C
recommendation. If you really want to remove the attributes, then you
can assign listeners to elements at run-time. However, this addition
will need to be deferred until the load event is fired.

var global = this;

function keypress(e) {
if((e = e || global.event)) {
/* Use e to refer to the event object */
}
}

if(document.getElementById) {
global.onload = function() {
var remarks = document.getElementById('remarks');

if(remarks) {
remarks.onchange = remarks.onkeypress
= remarks.onkeydown
= remarks.onkeyup
= keypress;
}
};
}
Does anyone know how I can successfully get the event object in a
javascript file using firefox so I can user event.which ???


No need to use event.which. Just use event.keyCode according to W3
standards.


Where did you read that? The only W3C DOM publication that relates to
keyboard events is contained in DOM 3 Events, which I believe has been
abandoned as a Working Group Note. Irrespective of its status, it
contains no mention of a keyCode property.

The keyCode property is not universal, so testing for it is useful:

if('number' == typeof e.keyCode) {
/* Use the keyCode property */
} else if('number' == typeof e.which) {
/* Use the which property */
}

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Sep 22 '05 #3

P: n/a
Michael Winter wrote:
On 22/09/2005 14:11, Robert wrote:
function keypress(event)
{
event = event || window.event; // window.event for IE

This construct is only necessary when a function object will be assigned
directly to a event property.


Yep sorry.
No need to use event.which. Just use event.keyCode according to W3
standards.

Where did you read that?


Ohh sigh. It was in the DOM2 Event recommendation, but they removed it :(
And it seem they will replace it with something else again in DOM3.
Not very convenient for us.

keyCode does work fine in IE6 and Firefox.
Sep 22 '05 #4

P: n/a
<ri************@talk21.com> wrote in message news:11**********************@z14g2000cwz.googlegr oups.com...
Help....

I've got a textarea where people type in a description. However for
certain reasons we need to stop them typing !$*^ .

I have a solution this which works fine in IE:

function keypress()
{

if((event.keyCode==33)||(event.keyCode==36)||(even t.keyCode==42)||(event.keyCode==94))
{
file://bad key pressed
event.returnValue = false;
}
}

This function is in a javascript file and called from the textarea like
this:

<textarea rows="13" maxLength="860" cols="30" name="remarks"
id="remarks" title="description"
onchange="keypress();" onKeypress="keypress();" onKeyDown="keypress();"
onKeyUp="keypress();"
value =""></textarea>

However, it doesn't work in Firefox. I changed event.keyCode to
event.which . However, this only works if the javascript is embedded in
the html and not in my .js file. However, one of the requirements for
my project is that it is W3c compliant and therefore the javascript
should be in separate files and not embedded in the html.

Does anyone know how I can successfully get the event object in a
javascript file using firefox so I can user event.which ???

Cheers...Rich

Would this be an acceptable alternative?

<TEXTAREA onchange='this.value=this.value.replace(/[\x24\^\!\*]/g,"");' >

--
S.C.
Sep 22 '05 #5

P: n/a
ri************@talk21.com a écrit :
Help....

I've got a textarea where people type in a description. However for
certain reasons we need to stop them typing !$*^ .

I have a solution this which works fine in IE:

function keypress()
{

if((event.keyCode==33)||(event.keyCode==36)||(even t.keyCode==42)||(event.keyCode==94))
{
//bad key pressed
event.returnValue = false;
}
}

This function is in a javascript file and called from the textarea like
this:

<textarea rows="13" maxLength="860" cols="30" name="remarks"
id="remarks" title="description"
onchange="keypress();" onKeypress="keypress();" onKeyDown="keypress();"
onKeyUp="keypress();"
value =""></textarea>
The above 4 event handlers is unnecessary. If you want to filter 4
precise printable characters (non-printable ones is another story), then
you only need to use the keypress event handler.

However, it doesn't work in Firefox. I changed event.keyCode to
event.which . However, this only works if the javascript is embedded in
the html and not in my .js file. However, one of the requirements for
my project is that it is W3c compliant and therefore the javascript
should be in separate files and not embedded in the html.

Does anyone know how I can successfully get the event object in a
javascript file using firefox so I can user event.which ???

Cheers...Rich


function init()
{
if(document.addEventListener) // DOM 2 Events compliant
{
document.getElementById("remarks").addEventListene r("keypress",
Filter4Characters, true);
}
else if(window.event)
{
document.getElementById("remarks").onkeypress = Filter4Characters;
};
}

function Filter4Characters(evt)
{
if(evt.charCode && (evt.charCode == 33 || evt.charCode == 36 ||
evt.charCode == 42 || evt.charCode == 94))
{
if(evt.preventDefault)
{
evt.preventDefault();
};
}
else if(event.keyCode && (event.keyCode == 33 || event.keyCode == 36 ||
event.keyCode == 42 || event.keyCode == 94))
{
if(event.returnValue)
{
event.returnValue = false;
};
};
}

....

<body onload="init();" ...>

Opera 8.5 supports keyCode, is DOM 2 Events compliant and matches IE 6
event model; so, here, you'll have to test this further.

Gérard
--
remove blah to email me
Sep 22 '05 #6

P: n/a

Gérard Talbot ha escrito:
ri************@talk21.com a écrit :
Help....

I've got a textarea where people type in a description. However for
certain reasons we need to stop them typing !$*^ .

I have a solution this which works fine in IE:

function keypress()
{

if((event.keyCode==33)||(event.keyCode==36)||(even t.keyCode==42)||(event.keyCode==94))
{
//bad key pressed
event.returnValue = false;
}
}

This function is in a javascript file and called from the textarea like
this:

<textarea rows="13" maxLength="860" cols="30" name="remarks"
id="remarks" title="description"
onchange="keypress();" onKeypress="keypress();" onKeyDown="keypress();"
onKeyUp="keypress();" value =""></textarea>


The above 4 event handlers is unnecessary. If you want to filter 4
precise printable characters (non-printable ones is another story), then
you only need to use the keypress event handler.

However, it doesn't work in Firefox. I changed event.keyCode to
event.which . However, this only works if the javascript is embedded in
the html and not in my .js file. However, one of the requirements for
my project is that it is W3c compliant and therefore the javascript
should be in separate files and not embedded in the html.

Does anyone know how I can successfully get the event object in a
javascript file using firefox so I can user event.which ???

Cheers...Rich


function init()
{
if(document.addEventListener) // DOM 2 Events compliant
{
document.getElementById("remarks").addEventListene r("keypress",
Filter4Characters, true);
}
else if(window.event)
{
document.getElementById("remarks").onkeypress = Filter4Characters;
};
}

function Filter4Characters(evt)
{
if(evt.charCode && (evt.charCode == 33 || evt.charCode == 36 ||
evt.charCode == 42 || evt.charCode == 94))
{
if(evt.preventDefault)
{
evt.preventDefault();
};
}
else if(event.keyCode && (event.keyCode == 33 || event.keyCode ==36 ||
event.keyCode == 42 || event.keyCode == 94))
{
if(event.returnValue)
{
event.returnValue = false;
};
};
}

...

<body onload="init();" ...>

Opera 8.5 supports keyCode, is DOM 2 Events compliant and matches IE 6
event model; so, here, you'll have to test this further.

Gérard
--
remove blah to email me

CHECKING KEYPRESS FIREFOX - ONLY NUMBERS

Hola no se si esto te servira

en mi HTML, JPG, etc

<INPUT TYPE="TEXT" onKeyPress="return checkNumero(event)">
luego en mi javascript

function CheckNumero(event)
{
var key=event.which;//codigo de tecla.

if (key==8) return true;

if (key < 48 || key > 57)
{ //si no es numero
return false
}
return true
}
Entonces con solo devolver false al input la key apretada no se
marcara. Solo con return true

Oct 6 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.