Java Reference
In-Depth Information
e.preventDefault();
break;
}
}
elementCount++;
}
}
document.form1.addEventListener("submit", checkForm);
</script>
</body>
</html>
Let's look at each error in turn. The first error is a logic error:
while(elementCount =< theForm.length)
Arrays start at
0
so the first
element
object is at index array
0
, the second at
1
, and so on. The
last 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
5
, but because 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 fine, though the first is shorter.
You come to your second error in the following line:
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 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() = "")
This has two errors. First,
value
is a property and not a method, so there is no need for parentheses
after it. Second, you 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 equals signs mean “Check if it is equal to.” So
with the changes, the line is:
if (theForm.elements[elementCount].value == "")