Solved

How to Cancel a Submit when Enter Key is Press?

Posted on 2003-10-30
8
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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
 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Suggested Courses

636 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