How to disable "Enter Key" to submit?

  • Thread starter http://links.i6networks.com
  • Start date
H

http://links.i6networks.com

I want to force the users to click submit to submit the forms. How do I
disable "Enter Key" which will submit the form automatically when they
entered the data in text field then pressed "enter key"
 
Y

Yann-Erwan Perio

http://links.i6networks.com said:
I want to force the users to click submit to submit the forms. How do I
disable "Enter Key" which will submit the form automatically when they
entered the data in text field then pressed "enter key"

You can refuse the enter key, so that it doesn't trigger the default
action, as follows.

---
<form action="foo">
<input type="text" onkeypress="return bar(event)">
<input type="submit">
</form>

<script type="text/javascript">
function bar(evt){
var k=evt.keyCode||evt.which;
return k!=13;
}
</script>
---

Note however that this "enter" key behavior isn't standard (and actually
behaves differently across user agents); moreover this won't prevent you
from full server-side checking, as a client can have javascript
deactivated (or even trick your javascript at runtime).


HTH,
Yep.
 
K

kaeli

I want to force the users to click submit to submit the forms. How do I
disable "Enter Key" which will submit the form automatically when they
entered the data in text field then pressed "enter key"

Out of curiosity, why?
I've noticed a lot of times, people who want to do this have put code in the
onClick of the submit button when it should really go in the onSubmit of the
form.

If using the enter key breaks your form and you feel the user MUST click a
button, and your users are known to have script enabled (intranet, etc), the
best way to prevent form submission with the enter key is to simply not
supply a submit button, action, or method at all. Have a normal button that
sets those values and submits when clicked (and keypress if you need to meet
accessibility guidelines). This is a great way to require script and a great
way to really piss off internet users. ;) Works well for intranet apps,
though.

--
--
~kaeli~
If a chicken and a half can lay an egg and a half in a day
and a half, how long would it take for a monkey with a
wooden leg to kick the dill seeds out of a pickle?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 
I

infinull

alternatively provide no submit button and a button that sends the form:
<form method="??" action="??">
<input ....
..../>
<input type="button" value="Submit" onclick="this.parent.submit()"/>

</form>
 
R

Randy Webb

infinull said:
alternatively provide no submit button and a button that sends the form:
<form method="??" action="??">
<input ....
..../>
<input type="button" value="Submit" onclick="this.parent.submit()"/>

this.form.submit(), not the this.parent.submit() I presume is what you
intended to say?
 
E

Evertjan.

infinull wrote on 18 aug 2004 in comp.lang.javascript:
alternatively provide no submit button and a button that sends the form:
<form method="??" action="??">
<input ....
.../>
<input type="button" value="Submit" onclick="this.parent.submit()"/>

</form>

include onsubmit="return false" in <form>:


<form onsubmit="return false">
<input/>
<input type="button" value="Submit"
onclick="this.form.submit()"/>
</form>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top