473,748 Members | 4,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 16663
Simon Wigzell wrote on 19 nov 2004 in comp.lang.javas cript:
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(e vent.keyCode==1 3)event.keyCode =9">

Changes the enter to a tab [=next field]

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

<input onkeydown="if(e vent.keyCode==1 3)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******** ************@19 4.109.133.29...
Simon Wigzell wrote on 19 nov 2004 in comp.lang.javas cript:
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(e vent.keyCode==1 3)event.keyCode =9">

Changes the enter to a tab [=next field]

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

<input onkeydown="if(e vent.keyCode==1 3)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.dt d">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>untitled </title>
<script type="text/javascript">
//<![CDATA[

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

//]]>
</script>
</head>
<body>
<form action="javascr ipt:alert('ok') " style="width:20 0px;">
<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
2824
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 submitted whether it's finished or not. Is there an easy way to correct this? My site only cares about IE.
2
2017
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 of some characters into a TextBox (ASP.NET) of a Web Form.
2
7210
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 server side textbox control to do something - eg change the words on label - when the key pressed is the "enter" key. I can see the label change to what i expect ----- BUT .... then the
2
1422
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 control this default behavior? I figured I could have an "are you sure?" confirmation if fields are blank, but I think I'd just rather make it so enter doesn't submit the form. Any examples or suggestions for me? Thanks! Matt
1
2000
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? 2. When I select a date in the calendar it appears in a text field but the focus is lost .I want to get the focus in another text field after selecting the date.What do I need to do? thanks
1
2370
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"> function search() { var search = document.getElementById('Search'); location = "search_response.php?Search=" + escape(search.value);
3
4170
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 button. How to make this script can be run on Firefox? I would be grateful to anybody who can help me on this, as I have got out of my depth with this problem. Thanks, Arya ======== This is the script : <script language="javascript"...
0
1665
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 results page is displayed. When I press the "enter" key nothing happens. I want to submit the form by pressing the "enter" key also. I have placed <input type="text" style="display:none"> after the <form> tag in the HTML section. Now if my cursor...
4
3879
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 list controls, one text box, one "GO" button, one "reset" button. When the "GO" button is clicked the form gets submitted and the results page is displayed. When I press the "enter" key nothing happens. I want to submit the form by pressing the "enter"...
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9541
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9370
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8242
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4602
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3312
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.