473,382 Members | 1,329 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

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?
Jul 23 '05 #1
4 16625
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)

Jul 23 '05 #2
Just add somewhere inside your form:
<input type="text" style="display:none">

Henri

"Evertjan." <ex**************@interxnl.net> a écrit dans le message de
news:Xn********************@194.109.133.29...
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)


Jul 23 '05 #3


<?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>
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #4
Simon Wigzell wrote:
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.
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: EBG | last post by:
I have a simple email form on my site but getting screwy results because I know alot of users are hitting "enter" instead of tab when doing the form. When they hit enter the entire form is...
2
by: marcia | last post by:
How can I capture (i.e., write an event handler) that runs after I press the "Enter" key? I'd like to prevent a Web Form from being submitted when the "Enter" key is pressed following the typing...
2
by: Cindy | last post by:
Hi all you smarties out there, I'm having a little conundrum with my asp.net page Scenario: I have a form (asp.net) with no code behind (as yet). I have placed a javascript function on a...
2
by: MattB | last post by:
I have a WebForm that has a "reset" button and an "Enter" button. It's a long form, and users tend to hit the enter key at some point when they really want to tab to the next form field. How can I...
1
by: Janice | last post by:
Hi, I have 2 questions please: 1. I have a calendar in javascript and it works well but every time I hit "enter" anywhere in my form it appears again.What do I need to do for this not to happen? ...
1
by: laredotornado | last post by:
Hi, I have a very simple form and I would like on Firefox and IE for the form to submit upon the user pressing enter in the textfield. Right now, I have <script type="text/javascript">...
3
by: aryayudhi | last post by:
I have a html page that has javascript that works perfectly in IE, but not in Firefox. The use of this javascript to change "Tab" to "Enter" Button. When we press Tab, it is like when we press Enter...
0
by: roxanaislam | last post by:
Hi: I have a search form in my application which has 4 dropdown list controls, one text box, one "GO" button, one "reset" button. When the "GO" button is clicked the form gets submitted and the...
4
by: roxanaislam | last post by:
Submitting Form by pressing the "ENTER" key -------------------------------------------------------------------------------- Hi: I have a search form in my application which has 4 dropdown...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.