preventing accidental form submission

  • Thread starter shortbackandsides.no
  • Start date
S

shortbackandsides.no

I've been having trouble preventing users pressing Enter part way down
a form so the incomplete form gets submitted.

I came up with a possible solution - the code below seems to work in
both mozilla and MSIE - is this a good way to solve the problem? Is
there a better alternative? Have I done anything stupid?

My aim was to disable the normal submit process then use javascript to
submit which appears to bypass that and work...

Neither the normal submit button nor pressing Enter result in the form
being sent (so in a real world application one would use a button
rather than the normal submit field type).

<HTML><HEAD><TITLE></TITLE></HEAD><BODY >
<FORM ACTION="http://www.example.com/cgi-bin/myprogram.pl"
METHOD="POST" name="myform" onSubmit="return false;">
<input type="text" size="45" name="afield" value="a field" ><BR>
<INPUT type="reset" value="Reset">
<INPUT type="button" value="Submit (button)"
onClick="document.myform.submit();">
<INPUT type="submit" value="Submit (normal)" >
</FORM></BODY></HTML>
 
M

Michael Winter

I've been having trouble preventing users pressing Enter part way down a
form so the incomplete form gets submitted.

I came up with a possible solution - the code below seems to work in
both mozilla and MSIE - is this a good way to solve the problem? Is
there a better alternative? Have I done anything stupid?

All you should try to do is check the values on both client and server. If
the form seems incomplete, display it again filled with the submitted
values and tell the user to complete the form before sending it. The user,
having any sense, will see that pressing Enter at the time they did isn't
a good idea, and it should be last time they do it.

No need to confuse the issue with multiple buttons, some working and some
not. Some dependent on scripting and some not.

[snip]

Mike
 
S

shortbackandsides.no

All you should try to do is check the values on both client and server. If
the form seems incomplete, display it again filled with the submitted
values and tell the user to complete the form before sending it. The user,
having any sense, will see that pressing Enter at the time they did isn't
a good idea, and it should be last time they do it.

I agree that is a better approach *where practicable* however it makes
the assumption that all the fields are required to be completed, this
is not always the case and this is the shortcoming I am trying to
address.
No need to confuse the issue with multiple buttons, some working and some
not. Some dependent on scripting and some not.
The example had two submit buttons in order to demonstrate the
difference in their operation, a real-world implementation would not.
 
R

RobG

I agree that is a better approach *where practicable* however it makes
the assumption that all the fields are required to be completed, this

Not at all. Mike is saying check the values on the client
to prevent a wasted trip to the server. If the user doesn't
have javascript enabled, you have to rely on server-side
validation and response.

His comment regarding "tell the user to complete the form
before sending it" is in the context of telling the user to
complete all mandatory fields, I don't think he means they
must complete every field necessarily. Hopefully you have
on-screen help and tips so that the user knows which fields
are mandatory and those that aren't and are guided as to
correct formats, etc.

Cheers, Rob.
 

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

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top