Robert S wrote:
I am using the following to prevent the Enter key from submitting my
form (this would have undesirable results):
Why not use onsubmit and a form validation routine?
(index.html)
<body onkeydown="rejectEnter( event )">
(functions.js)
function rejectEnter( oEvent )
{
if ( 13 == oEvent.keyCode )
{
try
{
oEvent.preventDefault();
}
catch ( oError )
{
oEvent.returnValue = false;
}
}
}
try..catch is really just a kludge where nothing better is available,
feature detection is all the rage so why not use it:
function rejectEnter(oEvent)
{
if ('number' == typeof oEvent.keyCode
&& 13 == oEvent.keyCode){
if ('function' == typeof oEvent.preventDefault){
oEvent.preventDefault();
} else {
oEvent.returnValue = false;
}
}
return false;
}
This works fine in Firefox, IE6 and Konqueror, but it does not reject
the keypress in Opera (9.0). Is there an easy way of doing this in
Opera using code similar to the above?
Presumably users are familiar with the fact that pressing 'enter' will
sumbit the form, why change the browser's default behaviour and confuse
them? Use an onsubmit handler and cancel the submit if they havn't
finished filling in the form.
It looks like it execultes the line "oEvent.preventDefault();", but it
does not have the desired effect.
Returning false from an onsubmit handler should work more reliably than
attempting to cancel default behaviour by intercepting key presses
(noting that client-side validation is helpful but unreliable).
--
Rob