Prevent "Enter" on a text field from submitting a form.

S

Simon Wigzell

Is there a way to prevent a form submitting when you press enter on a text
field? Some people press enter when they finish typing in a text field out
of habit I guess unconcsciously thinking it will take them to the next
field. Instead it submits the form and this causes all kinds of problems in
my case. Any javascript solution?
 
E

Evertjan.

Simon Wigzell wrote on 19 nov 2004 in comp.lang.javascript:
Is there a way to prevent a form submitting when you press enter on a
text field? Some people press enter when they finish typing in a text
field out of habit I guess unconcsciously thinking it will take them
to the next field. Instead it submits the form and this causes all
kinds of problems in my case. Any javascript solution?

I use this, but that is probably not cross browser campatible:

<input onkeydown="if(event.keyCode==13)event.keyCode=9">

Changes the enter to a tab [=next field]

=============

<input onkeydown="if(event.keyCode==13)event.keyCode=0">

Changes te enter to not action
 
H

Henri

Just add somewhere inside your form:
<input type="text" style="display:none">

Henri

Evertjan. said:
Simon Wigzell wrote on 19 nov 2004 in comp.lang.javascript:
Is there a way to prevent a form submitting when you press enter on a
text field? Some people press enter when they finish typing in a text
field out of habit I guess unconcsciously thinking it will take them
to the next field. Instead it submits the form and this causes all
kinds of problems in my case. Any javascript solution?

I use this, but that is probably not cross browser campatible:

<input onkeydown="if(event.keyCode==13)event.keyCode=9">

Changes the enter to a tab [=next field]

=============

<input onkeydown="if(event.keyCode==13)event.keyCode=0">

Changes te enter to not action

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)
 
R

Rob B

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>untitled</title>
<script type="text/javascript">
//<![CDATA[

document.onkeypress = function(e)
{
e = e || window.event;
if (typeof e != 'undefined')
{
var tgt = e.target || e.srcElement;
if (typeof tgt != 'undefined' && /input/i.test(tgt.nodeName))
return (typeof e.keyCode != 'undefined') ? e.keyCode != 13 : true;
}
}

//]]>
</script>
</head>
<body>
<form action="javascript:alert('ok')" style="width:200px;">
<input type="text" name="t1" value="" />
<input type="text" name="t2" value="" />
<input type="submit" />
</form>
</body>
</html>
 
F

Fred Oz

Simon said:
Is there a way to prevent a form submitting when you press enter on a text
field? Some people press enter when they finish typing in a text field out
of habit I guess unconcsciously thinking it will take them to the next
field. Instead it submits the form and this causes all kinds of problems in
my case. Any javascript solution?
Yes. When the form is submitted, validate the content. If it's not
finished or correct, return the user to the form. They will soon learn
not to press enter to move to the next field.

Changing the user interface so that "enter" or "return" equals "tab"
just makes your interface different from the rest of the web. Your
users will be confused that when they've finished the form, pressing
enter doesn't submit it.

Incidentally, enter does not submit the form on all browsers.

Rob.
 

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

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top