marc_rabil
asked on
How to Cancel a Submit when Enter Key is Press?
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="https://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
<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="https://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
<script>
function KeyPress() {
if (window.event.keyCode == 13) {
window.event.keyCode = 0;
}
}
</script>
<body onKeyPress="KeyPress();">
function KeyPress() {
if (window.event.keyCode == 13) {
window.event.keyCode = 0;
}
}
</script>
<body onKeyPress="KeyPress();">
IE only sybe
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>
==========================
<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>
ASKER
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>
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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
marc_rabil, how are you?
http:Q_11603498.html