Preventing form submission with return
Home / Site Building & Testing / Preventing form submission with return
Here's a quickie.
Is there an accessible method to prevent the enter key from submitting a form, while allowing the enter key to work as normal on focused links and the submit button?
regards
mike 2k:)2
_________________
<marquee><blink><work> webSemantics </work><rest> 2kool2 </rest> & <play> bangers & mashed </play></marquee></blink>
http://www.quirksmode.org/...
Let me know if you need an example. I don't know how familiar with Javascript you are?
I suspect you're really after a scriptless solution, in which case I can't think of any.
| kiwibrit wrote: |
| Well you could capture the submission server-side, then re-present it filled in, and take no further action. If you had the Javascript you described, that would prevent submission in the first place for most, yet cover the case for those who did not have Javascript enabled. |
True, though that does seem a lot of work for very little gain.
If it's not being too nosy, can I ask why you want to do this Mike? Are you simply trying to prevent accidental submission of the form, or is this something more fundamental than that?
Not sure this is a very accessible thing to be doing though. But I don't know the situation...
_________________
creator of Talklets
Talklets ,
I am looking for a client side JavaScript solution.
It's a large data entry type form and the biggest user "mistake" is pressing return while in an input text field.
Not the end of the world but it's very irritating to the user who has to wait for the server response.
I've been toying with placing onsubmit="return false" in the form element and adding onclick="check_and_submit()" to the submit button.
Though I'm unhappy with the consequences of this method.
I tried using a simple event trap to test keyboard for 13 (Enter / return).
But couldn't get it to work cross browser, and didn't get as far as limiting it's scope.
Torsten,
My JavaScript is quite good (level 5 plus).
I'm just working through the article posted (advanced events).
If you have any examples that would be brilliant.
regards
mike 2k:)2
_________________
<marquee><blink><work> webSemantics </work><rest> 2kool2 </rest> & <play> bangers & mashed </play></marquee></blink>
| mike 2k:)2 wrote: |
| I tried using a simple event trap to test keyboard for 13 (Enter / return).
But couldn't get it to work cross browser, and didn't get as far as limiting it's scope. |
Due to various problems, a temp solution for the search was put in place on www.macmillan.org.uk, if you check the the opensearch.js file near the bottom, it's got just what you're after.
Attaching that type of event to just the inputs (although not the submit obviously) should sort it out.
_________________
Nomensa / AlastairC
Bill, the six levels of javascript though I've seen a seventh proposed somewhere. It's quite funny.
_________________
<marquee><blink><work> webSemantics </work><rest> 2kool2 </rest> & <play> bangers & mashed </play></marquee></blink>
Last edited by mike 2k:)2 on 15 Mar 2007 04:08 pm; edited 1 time in total
| Code: |
| if (window.event.keyCode == 13)
{ event.returnValue=false; event.cancel = true; } |
p.s. checkout http://www.beansoftware.com/... for pure asp.net solutions and variations.
Tried that but it doesn't work cross browser.
_________________
<marquee><blink><work> webSemantics </work><rest> 2kool2 </rest> & <play> bangers & mashed </play></marquee></blink>
| Code: |
| function cancelEvent(e) {
if (e && e.preventDefault) { e.preventDefault(); } return false; } |
_________________
Jim O'Donnell
work: Royal Observatory Greenwich
play: eatyourgreens
This works in all the browsers I have access to at the mo (O9, FF 2 and IE6) though it's not very elegant and may not play well with other scripts.
| Code: |
| window.onload = function() {
var textfields = document.getElementsByTagName('input'); for (var i=0, count=textfields.length; i<count; i++) { if (textfields[i].type == 'text') { textfields[i].onkeypress = function(e) { if (!e) var e = window.event; if (e.keyCode == 13) return false; } } } } |
All times are GMT
You cannot post new topics in this forumYou cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


