Disable "Enter" key in a form

A

Anthony

Hello again everybody,

Since i know the experts on frontpage reside here, i feld free to post
another question.
Here it is, How do i disable the "Enter" on a page were people fill in a
form.
Lots of people try to 'jump' to the next field in my form by using the
'enter' key.
Problem is that this key is behaving the same as the 'Submit' button
I can not use validation on all textfields cause some of them can be left
empty
by visitors.
Any help will be very much appreciated (again)
 
K

Kevin Spencer

function captureKey(e) {
var k = document.all? window.event.keyCode:e.which;
return k != 13;
}
document.onkeypress = captureKey;
if (document.layers) document.captureEvents(Event.KEYPRESS);

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
A

Anthony

Thanks Kevin so far, but can you tell me where on my
page i put this 'code' please.
I tried lots of things but negative results, Don't i have to
put tags somewhere ?
Thnx Again.
 
K

Kevin Spencer

Put it anywhere between the <body> and </body> tags on the page.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
A

arno

Hi Kevin,

your code also deletes ENTER in "textareas". Is there a workaround for this?

I'd wrap your function with something like: if I am in
document.myform.field "my textarea" then do nothing, otherwise ignore ENTER.
However, I do not know how I can find out what the "active" field of the
form is.
function captureKey(e) {
******if active field is not "my textarea then
var k = document.all? window.event.keyCode:e.which;
return k != 13;
}
document.onkeypress = captureKey;
if (document.layers) document.captureEvents(Event.KEYPRESS);
*******else do nothing (= allow ENTER)

best regars

arno
 
A

arno

Hi Anthony,
Lots of people try to 'jump' to the next field in my form by using the
'enter' key.

I found a simple way to make "enter" jump to the next field instead of
sending the form:
http://javascript.internet.com/forms/enter-key-focus.html

it says basically to use
onKeyDown="if(event.keyCode==13) event.keyCode=9;"
where you want to disable enter=send.

eg. for your address field:

<FORM METHOD="POST">
Address: <INPUT TYPE="TEXT" onKeyDown="if(event.keyCode==13)
event.keyCode=9;"><BR>
<INPUT TYPE="submit" Value="Ok">
<INPUT TYPE="reset" Value="Cancel">
</FORM>


regards

arno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top