Solved

How to Cancel a Submit when Enter Key is Press?

Posted on 2003-10-30
8
168 Views
Last Modified: 2012-05-04
I have a situation as shown in the example below .  I have a form with an action and a submit button.  For most fields on the form (e.g. input_2 below), I am happy with the behavior that the enter key will cause the form to submit.  However for some special fields (e.g. input_1 below), I want to trap the Enter key and not submit the form.

<HTML>
<head>
<script language="JavaScript">
<!--
function keyDownFx(e) {
  // Some code that traps the enter key...
  alert("keyDownFx called...");
  return false;
}
//-->
</script>
</head>
<body>
<form name="form" action="http://www.experts-exchange.com">
<input name="input_1" onkeydown="keyDownFx(event);">
<br>
<input name="input_2">
<br><input type="submit">
</form>
</body>
</HTML>

I have been testing in IE6, but I would really like a solution that is capable of running in NN4+ and IE4+.  Thanks in advance,

Marc
0
Comment
Question by:marc_rabil
8 Comments
 
LVL 16

Expert Comment

by:jaysolomon
ID: 9650542
0
 
LVL 28

Expert Comment

by:sybe
ID: 9650761
<script>
function KeyPress() {
    if (window.event.keyCode == 13) {
        window.event.keyCode = 0;
    }
}
</script>
<body onKeyPress="KeyPress();">
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 9650792
IE only sybe
0
 
LVL 25

Expert Comment

by:devic
ID: 9651026
hurra i am elated and have good idea ;)

====================================
<html>
<head>
<script>
// ;) devic
var isbutton=false;
function notenter()
{
      if(isbutton)return true;
      return false;
}
</script>
</head>

<body>


<form action=http://www.google.com/search onsubmit="return notenter()">
      <input type=text value=hurra name=q>
      <input type=submit onclick="isbutton=true;">
</form>

</body>
</html>
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:marc_rabil
ID: 9651520
Devic,

If I modify your code with another input (as below), it no longer works (on IE6 anyway). Any idea why?

<html>
<head>
<script>
// ;) devic
var isbutton=false;
function notenter()
{
     if(isbutton)return true;
     return false;
}
</script>
</head>

<body>


<form action=http://www.google.com/search onsubmit="return notenter()">
     <input type=text value=hurra name=q>
     <br><input>
     <input type=submit onclick="isbutton=true;">
</form>

</body>
</html>
0
 
LVL 25

Accepted Solution

by:
devic earned 125 total points
ID: 9651668
oops, Marc i don't understood yet why.

try this:
=====================
<html>
<head>
<script>
// ;) devic
var isbutton=false;
function notenter()
{
   // alert(isbutton)
      if(isbutton)return true;
    return false;
}
</script>
</head>

<body>


<form action=http://www.google.com/search onsubmit="return notenter()">
    <input type=text value=hurra name=q>
      <input type=text value=hurra name=b>
      <input type=text value=hurra name=c>
      <input type=text value=hurra name=d>
    <input type=submit onmouseover="isbutton=true;" onmouseout="isbutton=false;">
</form>

</body>
</html>
0
 
LVL 25

Expert Comment

by:devic
ID: 9651693
anyway i think onmouseover-out is better as onclick, because if you have some validation, you need always change isbutton=, if data is not valid
0
 
LVL 25

Expert Comment

by:devic
ID: 9663220
marc_rabil, how are you?
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now