[thelist] dHTML Form Elements && tip re: Disabling the <enter > key to submit a form.

Aylard JA (James) jaylard at equilon.com
Fri Dec 1 13:40:46 CST 2000


> Ok, so if a non-js user submits a form that has onsubmit() validation, the
> validation would just get ignored and the form would submit normally,

	That's right.

> Well, that wouldn't solve the original question of how to disable the
> <enter> key for form submission, so I guess the consensus is that you
> without JavaScript.

	Microsoft's MSDN site states that adding a second form to a page
will prevent the enter key from submitting a form
although in my testing on IE 5.01 SP1, it didn't make any difference.

> For purposes of answering that specific question, I like my solution
> than Jeff's, because Jeff's will pop up some alert box telling the user
> didn't fill out some field before they (accidentally?) hit <enter>, and
> will simply ignore the <enter> key altogether.  But I agree that caution
> should be exercised before deciding to use my method, as it may do more
> than good in some cases.

	Part of that harm could be that people come to expect the enter key
to submit a form, and are confused when it doesn't work. You might well be
creating a new usability issue, and not merely solving an existing one,
perceived or real. And since it doesn't appear that you can prevent the
enter key from submitting a form without using scripting, IMO it is better
to catch missing fields in an onsubmit even handler and alert the user. That
way you still prevent premature form submission without interfering with the
default behavior of the browser.

> And finally, I'm not saying that you should always use functions with
> submit() to handle form submissions, or even that it's a good habit to get
> into.  I'm just saying you shouldn't always rule it out either...sometimes
> it really can be exactly what you need.

	I'm not one for ironclad development rules, either. There may be
times when using the submit() method is appropriate, although I would argue
that they are likely rare. Most often, there is more than one way to do
things, and sometimes the advantages and disadvantages of each approach
balance one another out. Sometimes, though, they don't balance out, and the
time taken to explore a new approach can pay off.

> (jeez, see if I ever try to leave a tip around here again...)  ;)

	Nah, keep on postin'. It's been a good exchange. And not too much
blood has been spilled. :)

My 2¢.
James Aylard

More information about the thelist mailing list