Solved

How to Cancel a Submit when Enter Key is Press?

Posted on 2003-10-30
8
169 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

914 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

16 Experts available now in Live!

Get 1:1 Help Now