Solved

PREVENT FORM TO SUBMIT WHEN ENTER KEY IS PRESSED

Posted on 2001-08-09
11
421 Views
Last Modified: 2007-12-19
Hi,

  Facing problems when a form gets submitted when user hits enter in a text field. How can you prevent submit when enter is pressed on a text field.

Thanks
0
Comment
Question by:badami
  • 5
  • 2
  • 2
  • +2
11 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 6369782
return false from the onSubmit:

<FORM name='myform' action='myaction.asp' method='post'
  onSubmit='return false;'>
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 6369787
... and make your submit button force the issue:

<INPUT type='button' value='Submit' onClick='this.form.submit();'>

</FORM>
0
 
LVL 2

Expert Comment

by:MCM
ID: 6369812
I think I've found <FORM onSubmit> to fire unreliably. I trap for the enter key in the onKeyPress event of every text box:

<INPUT type="TEXTBOX" onKeyPress="{if keyWasEnter(event) {return false;}}">

The "return false" cancels the key press and thus avoids the submission. The function called is defined below. I pass the event object around for NS, IE grabs the keypress code from the window.event.keycode (the (document.all) tests to see if the client is IE or NS pretty reliably, i think.

<SCRIPT language="javascript">
<!--
function keyPressedCode(evt)
     {
     var ret;
     if (document.all)
          {
          ret=window.event.keyCode;
          }
     else
          {
          ret=evt.which;
          };
     return ret;
     };

function keyWas(keyNum, evt) {return (keyPressedCode(evt)==keyNum);     }
function keyWasEnter(evt)      {return (keyWas(13, evt));};

//-->
</SCRIPT>
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:badami
ID: 6369961
Hi MCM,

   I am using Netscape? Communicator 4.73 but this script doesn't seem to work it doesn't detect the onKeyPress

I have enabled my Javascript.

0
 

Author Comment

by:badami
ID: 6369966
Hi MCM,

   I am using Netscape? Communicator 4.73 but this script doesn't seem to work it doesn't detect the onKeyPress

I have enabled my Javascript.

0
 

Author Comment

by:badami
ID: 6369967
Hi MCM,

   I am using Netscape? Communicator 4.73 but this script doesn't seem to work it doesn't detect the onKeyPress

I have enabled my Javascript.

0
 

Author Comment

by:badami
ID: 6369975
Hi MCM,

   I am using Netscape? Communicator 4.73 but this script doesn't seem to work it doesn't detect the onKeyPress

I have enabled my Javascript.

0
 

Author Comment

by:badami
ID: 6369979
Hi MCM,

   I am using Netscape? Communicator 4.73 but this script doesn't seem to work it doesn't detect the onKeyPress

I have enabled my Javascript.

0
 
LVL 2

Expert Comment

by:MCM
ID: 6370063
drat. I thought I had it working. try putting onkeypress in lowercase, and I'll take another look at what I'm doing to see if can spot the problem.

is this the only INPUT in the form? Netscape might always submit on enter when there is only one INPUT in the form. But I might have made that up. I'll poke around some more.
0
 
LVL 1

Accepted Solution

by:
FranzRinkleff earned 25 total points
ID: 6370135
i do not like it when i submit a form by hitting the enter key because usually i did not intend to submit
the form.  therefore, i usually disable the enter key on those form objects that result in the form
submitting when the enter key is clicked.  

i use the following javascript function.

function disableEnterKey()
{
return !(window.event && window.event.keyCode == 13);
}

in the form objects such as the input fields.  

<input type="text" name="inputField" value="xyz"
onkeypress="return disableEnterKey()">

i even disable the submit button

<input type="submit" name="submitPlanButton" value="Submit Plan" onkeypress="return disableEnterKey()">
                                 
by doing this the user will only submit the form by clicking on the submit button with the mouse.  

the enter key will still work in textareas.

not sure if you are familiar with javascript.

if not put the javascript function between your head tags.

like this with any of your other head elements

<head>
<script language="javascript">
  <!--
    function disableEnterKey()
    {
      return !(window.event && window.event.keyCode == 13);
    }
  //-->
 </script>
</head>


</head>
0
 
LVL 1

Expert Comment

by:Y_ee
ID: 6370150
try this :

<html>
<head>
     <title></title>
    <script>
    var ie=Boolean(document.all);
    var ns4=Boolean(document.layers && !document.getElementById);
    var gecko=Boolean(!document.all && document.getElementById);
    keyHandler=function(e) {
        var oKey;
        if (ns4 || gecko) oKey=e.which;
        else oKey=window.event.keyCode;
        if (oKey==13) return false;
    }
   
    if (ie) document.attachEvent('onkeyup',keyHandler);
    else if (ns4) {
        document.onkeyup=keyHandler;
        document.captureEvents(Event.KEYUP);
    } else {
        document.addEventListener('keypress',keyHandler,true);
        document.focus();
    }
    </script>
</head>
<body></body>
</html>

ciao
Y
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

856 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