Java Reference
In-Depth Information
}
</script>
<form name=”form1” onsubmit=”return checkForm(document.form1)” action=”“>
<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 fi rst error is a logic error.
while(elementCount =< theForm.length)
Arrays start at
0
so the fi rst
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 fi ve elements
in the form. So the
while
loop will continue until
elementCount
is less than or equal to
5
, but as the
last element has an index of
4
, this is one past the limit. You should write either this:
while(elementCount < theForm.length)
or this:
while(elementCount <= theForm.length - 1)
Either is fi ne, though the fi rst is shorter.
You come to your second error in the following line:
if (theForm.elements[elementcount].type == “text”)
On a quick glance it looks fi ne, but it's JavaScript's strictness on case sensitivity that has caused the
downfall. The variable name is
elementCount
, not
elementcount
with a lowercase c. So this line
should read as follows:
if (theForm.elements[elementCount].type == “text”)
The next line with an error is this:
if (theForm.elements[elementCount].value() = “”)