1. About.com
  2. Computing & Technology
  3. JavaScript

Validate on Submit

4. Avoid Multiple Submits

Join the Discussion

Questions? Comments?

We can stop the form being accidentally submitted twice by testing for it as part of the validation routine.

Some modern browsers allow us to disable the submit button once the form is submitted. We can do this by setting the disabled property of the button to true like this:

form.submitit.disabled=true;

Unfortunately this doesn't work with all browsers though. To get around this we can set up a field (let's call it 'submitted') that we set to '0' when the page is loaded. We then change this to '1' when the form is submitted and add an extra validation before the other field validations to ensure that the form has not already been submitted.

The main validation routine with both of these options added looks like this:

var submitted = 0;
function formvalidation(form) {
if (submitted) {
alert("Form already submitted, please be patient");
return false;}

// field validations go here

if (!submitted) {
form.submitit.disabled=true;
submitted = 1;
form.submit();}
}

All that this form validation script is missing is the validation for the other fields on the form that we have already discussed.

1 | 2 | 3 | 4

©2012 About.com. All rights reserved. 

A part of The New York Times Company.