Developer Support »

Preventing the form from being submitted when hitting enter

Applies to both scripts.

In some cases, you might find that your form is getting submitted too soon, and you are only getting partial form data submitted. This can be caused by the visitor hitting enter when filling out the form. Such is the way that browsers behave, a form will get submitted when hitting enter. If using the Pro version of the script, you can set required fields. This will prevent the form from being submitted if the required fields are empty.

One solution to this problem is to use JavaScript to make an alert box pop up when the visitor hits enter to submit the form. The alert box asks the visitor to confirm if they want to send the form at that time. To use this solution, you just need to add a little JavaScript code to your form page and to the <form> tag.

Open up your form page in a text editor and paste in this code:

<script type="text/javascript">
function checkForm(){if(!confirm('Are you sure that you want to submit the form now?\n\nThis alert is to prevent you sending the form accidentally by hitting the return key.\n\nIt still appears when you hit the submit button.\n\nThis does not mean that there is a problem with your form.'))return false;}

Next, paste this code into your <form> tag:

onSubmit="return checkForm();"

So for example, if you have this opening form tag:

<form action="formtoemailpro.php" method="post">

Change it to this:

<form action="formtoemailpro.php" method="post" onSubmit="return checkForm();">

That's it. Save the form page and upload it to your webspace.

You can change the wording (which you can see in the JavaScript code above) in the alert message to suit.