1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

Validate on Submit

1. Why Validate

When you include a form on one of your web pages, there will usually be one or more fields where you wish to perform some sort of validation before allowing the form to be submitted. This validation may be as simple as ensuring that something is entered in the field or it may be dozens of lines of code checking that something such as an email address is properly formed. You may include calls to Javascript validation routines to validate each of these fields using the onblur method to perform the validation when your visitor leaves the field but this doesn't validate the field if they never accessed the field directly in the first place.

The only way to ensure that all fields are validated before the form is submitted is to attach the validation to the submit button itself. This requires a few simple changes to the coding of your form and the javascript calls associated with it.

The first thing that you will want to do is to name your form. This will make it easier to reference the form from within your javascript. Also if you have more than one form on the page you will need to give each one a different name so that they can each be referenced separately. For example if you decide to name your form myform then the form statement might read as follows:

<form name="myform" action="email.php" method="post">

The action parameter, as usual, specifies the server side script or page that will process the form data once it has been validated (in this instance the generic PHP form processor that I wrote that converts forms into emails).

The other change that you need to make is to the submit button to get it to call your validation routine which in turn will submit the form if it is valid rather than submitting the form directly.

1 | 2 | 3 | 4

Note that the script and the descriptions of the functions that it contains are rewritten from the "Ask Felgall" website with the permission of the copyright owner.

©2016 About.com. All rights reserved.