469,271 Members | 1,191 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

How to disable "Enter Key" to submit?

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"
Jul 23 '05 #1
5 32956
http://links.i6networks.com wrote:
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.

Jul 23 '05 #2
In article <JX****************@news04.bloor.is.net.cable.roge rs.com>,
Sa***************@alexa.com enlightened us with...
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

Jul 23 '05 #3
kaeli <ti******@NOSPAM.comcast.net> wrote in message news:<MP************************@nntp.lucent.com>. ..
In article <JX****************@news04.bloor.is.net.cable.roge rs.com>,
Sa***************@alexa.com enlightened us with...
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"


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>
Jul 23 '05 #4
infinull wrote:
kaeli <ti******@NOSPAM.comcast.net> wrote in message news:<MP************************@nntp.lucent.com>. ..
In article <JX****************@news04.bloor.is.net.cable.roge rs.com>,
Sa***************@alexa.com enlightened us with...
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"

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?
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #5
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>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Job Lot | last post: by
2 posts views Thread by Gernot Frisch | last post: by
1 post views Thread by Holger (David) Wagner | last post: by
2 posts views Thread by chuckdfoster | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.