HTML and CSS Reference
In-Depth Information
if (document.forms[0].userAge.value < 18)
{
alert("Age is less than 18. You are not an adult.");
return false;
} // end if
alert("Name and age are valid.");
return true;
} // end function validateForm
// ]]> -->
</script>
Save the document and refresh it in the browser window. Click the submit button without
entering data in the Name or Age input boxes. Figure 14.21 shows the alert message
displayed when no data has been input and the submit button has been clicked.
Figure 14.21
The
validateform.html file
displayed in the
browser, without
input in the Name
and Age boxes; the
alert message
appears after the
form is submitted
Click the OK button, enter some text in the Name input box, and submit the form
again. Figure 14.22 shows data in the Name input box and the alert message that
appears due to validating the age. The age input box does not contain an age, and this
is interpreted as a value of 0.
Figure 14.22
The
validateform.html file
displayed in the
browser, with input
in the Name box
and without input in
the Age box; the
alert message
appears after the
submit button is
clicked
Search WWH ::




Custom Search