Calling the appropriate form
If you provide your HTML forms names, the name of the form can also be used and will be easier for many webmasters. Simply replace the position of forms[x] with the form name. For example, if a form’s name was ‘feedback’, calling that specific form within the document would be written like this:
Each form element belongs, or lives, within one single form, so when error checking multiple forms on one single page, no discrepancies will surface. When checking a particular form field, we call it by the name and check its value. So, let’s say we have the following form item in our first document form:
<input type=”text” Name=”FirstName”>
<form action=”test.php” method=”post” onSubmit=”return checkme()”>
// The following sets the cursor automatically in the FirstName text box field
function focus() // Define function focus
function checkme() // Define function checkme
Beyond the focus() function, we first use an if statement to check whether the input element FirstName is blank. If it is, an error will be displayed in the form of a popup window and the form will not be processed. You can simply copy and paste for any form element in your form, changing the field name, of course.
Putting it all together
Alright, I’ve broken the code down into sections, so let’s bring this code together into one HTML document. Here’s what a simple form would look like with three text boxes.
function checkme() //check for required fields
if (document.forms.MiddleName.value == “”)
if (document.forms.LastName.value == “”)
<form action=test.php method=post onSubmit=”return checkme()” name=Feedback>
Steve Adcock is a web developer and a recent college graduate and systems analyst. Steve has 9 years of web development experience and has skllis in HTML, CSS, PHP and MySQL, along with a good working knowledge of Adobe Photoshop and a keen understanding of web design techniques. Steve also runs www.websitegravy.com, a completely free, not for profit technology writing center aimed at taking the ambiguity out of computers and web design.