...

View Full Version : Stopping <Enter> from submitting in Netscape



bspahr75
04-11-2003, 02:46 PM
I am using the following function to prevent the user from submitting a form with the enter key. It work fine in IE, but not in Netscape. Anyone know of a fix for this?


<script type="text/javascript">
function checkKey( e )
{
var kc = ( e ) ? e.which : event.keyCode;
return ( kc != 13 );
}
</script>


<form onkeypress="return checkKey()">
<input type="text" name="" />
<input type="submit" name="Submit" value="Submit" />
</form>

beetle
04-11-2003, 03:18 PM
Which version of netscape? You always have to specify that these days as NS4.x and NS6+ are two different beasts.

bspahr75
04-11-2003, 03:20 PM
NS6+

beetle
04-11-2003, 03:48 PM
Oops. Sorry, I forgot the event only gets passed with script-assigned events.

This should work
<script type="text/javascript">

function checkKey( e )
{
var kc = e.which || e.keyCode;
return ( kc != 13 );
}

</script>


<form onkeypress="checkKey( event )">

brothercake
04-11-2003, 03:51 PM
Originally posted by bspahr75
I am using the following function to prevent the user from submitting a form with the enter key.
I strongly advise against this. If you prevent the enter key from submitting a form you're making it inaccessible to people who don't use a mouse. Remember that some people can't use a mouse.

Why, in any case, would you want to do this?

beetle
04-11-2003, 03:57 PM
Hey bcake, I thought you might be popping in here :p

I think this is another case of the client making a fairly adamant request. Since you're an accessibility guy, tell me this: would it be kosher to allow enter-key submissions when the Submit button has focus, but at no other time?

I have no concept of how many people know/don't know that the enter key will submit a form.

bspahr75
04-11-2003, 04:09 PM
I doesn't work on Netscape 6.2.3

beetle
04-11-2003, 04:25 PM
Hey, bspahr75, throw me a bone here. At least tell me what error message you are getting. :rolleyes:

Any feedback on the accessibility issue brothercake raised?

bspahr75
04-11-2003, 04:34 PM
I am not getting any error.

You were right about the unreasonable client request.

They just don't want anyone to be able to submit the form accidentally by pressing the enter key.

Don't most people use the tab key anyway?

brothercake
04-11-2003, 04:58 PM
Originally posted by beetle
HI think this is another case of the client making a fairly adamant request. Since you're an accessibility guy, tell me this: would it be kosher to allow enter-key submissions when the Submit button has focus, but at no other time?
I don't think so; it would certainly be better, but I see no valid reason for doing it at all - by catering for one potential problem, you create another actual problem, and a far more serious problem to boot. Lots of people use tab to navigate and enter to submit a form - power users as much as disabled visitors.

I would offer no compromise - the client is wrong. Point them towards the requirements of Section 508 of the Americans with Disabilities Act; they have no choice but to comply.

beetle
04-11-2003, 05:04 PM
Werd. I agree.

Maybe use the confirm message I suggested earlier?

brothercake
04-11-2003, 05:08 PM
Originally posted by beetle
Werd. I agree.
Is that so weird? ;)

I recommend pre-processing the form - send the data to a page which displays it and says "is all of this okay"? or something. Then you give two options - "yes, submit this information" or "no, take me back to my form".

bspahr75
04-11-2003, 05:22 PM
Yeah, is probably the best way to go.

Vladdy
04-11-2003, 05:23 PM
"Premature" form submission would mean that not all the fields were filled. So what's wrong with making sure that all the required fields have a valid value (and submit form only when they do) regardless which way ("enter" key or "submit" button) it was submitted. :confused: :confused: :confused:

Look in the ROOT of the problem :D :D :D

brothercake
04-11-2003, 05:33 PM
Originally posted by Vladdy
"So what's wrong with making sure that all the required fields have a valid value (and submit form only when they do)
Nothing wrong; an excellent solution.

But mine has an added usability benefit - once the form is submitted users get a second chance to make sure it's all okay - my experience is that reading what you wrote in a read-only output is more crystalising than reading it in an edit box. It's particularly useful for when you're sending precise data - like credit card numbers - or selecting from a complicated set of options.

I guess it depends; might be overkill for a very simple form.

Or you could do both - alert for missing data and post to a "confirm or edit" page. afaik there are no accessibility problems inherent with javascript alerts - but you do have to do something useful with the return ...

... CF is actually a rather bad example of this (ahem :p) - when you click "login" it says "please scroll to the bottom of this page" :( The link should take you to the login form - using an anchor for non-JS browsers, or sending focus to the first element for JS browsers.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum