The bug-free version looks like this:
<html>
<head>
</head>
<body>
<script language="JavaScript">
function checkForm(theForm)
{
var formValid = true;
var elementCount = 0;
while(elementCount < theForm.length)
{
if (theForm.elements[elementCount].type == "text")
{
if (theForm.elements[elementCount].value == "")
{
alert("Please complete all form elements")
theForm.elements[elementCount].focus();
formValid = false;
break;
}
}
elementCount++;
}
return formValid;
}
</script>
<form name="form1" onsubmit="return checkForm(document.form1)">
<input type="text" id="text1" name="text1">
<br>
CheckBox 1<input type="checkbox" id="checkbox2" name="checkbox2">
<br>
CheckBox 1<input type="checkbox" id="checkbox1" name="checkbox1">
<br>
<input type="text" id="text2" name="text2">
<P>
<input type="submit" value="Submit" id="submit1" name="submit1">
</P>
</form>
</body>
</html>
Let's look at each error in turn.
The first error is a logic error.
while(elementCount =< theForm.length)
Arrays start at zero so the first Form object is at index array 0, the second at 1, and so on. The last Form object has an index value of 4. However, theForm.length will return 5 because there are five elements in the form. So the while loop will continue until elementCount is less than or equal to five, but as the last element has an index of 4, this is one past the limit. We should either write
while(elementCount < theForm.length)
or
while(elementCount <= theForm.length - 1)
Either is fine, though the first is shorter.
We come to our second error in
if (theForm.elements[elementcount].type == "text")
On a quick glance it looks fine, but it's JavaScript's strictness on case sensitivity that has caused our downfall. The variable name is elementCount, not elementcount with a lowercase c. So this line should read
if (theForm.elements[elementCount].type == "text")
The next line with an error is this:
if (theForm.elements[elementCount].value() = "")
This has two errors. First, value is a property and not a method, so there is no need for parentheses after it. Second, we have the all-time classic error of one equals sign instead of two. Remember that one equals sign means make it equal to, and two equal signs means check if it is equal to. So with the changes, the line is
if (theForm.elements[elementCount].value == "")
The next error is our failure to put our block of if code in curly braces. Even though JavaScript won't throw an error since the syntax is fine, the logic is not so fine, and we won't get the results we expect. With the braces, the if statement should be as follows:
if (theForm.elements[elementCount].value == "")
{
alert("Please complete all form elements")
theForm.elements[elementCount].focus;
formValid = false;
break;
}
The penultimate error is in
theForm.elements[elementCount].focus;
This time we have a method but with no parentheses after it. Even methods that have no parameters must have the empty parentheses after them. So, corrected, the line is
theForm.elements[elementCount].focus();
Now, we're almost done; there is just one more error. This time it's not something wrong with what's there, but rather something very important that should be there but is missing. What is it? It's
elementCount++;
which should be in our while loop, otherwise elementCount will never go above 0 and the while loop's condition will always be true, resulting in the loop continuing forever; a classic infinite loop.
|