(^558) Chapter 14 A Brief Look at JavaScript
JavaScript code to indicate the value that will be sent back. Our example will return a
value of trueif the data is valid, the function will return a value of trueand a value of
falseif the data does not pass our validation tests. Notice that the onsubmitevent
handler also contains the keyword return. It works like this: if the validateForm()
function returns a value of true, the onsubmitevent handler becomes return true
and the form is submitted. If the validateForm()function returns a value of false,
theonsubmitevent handler becomes return falseand the form is not submitted.
Once a function returns a value, it is finished executing regardless of whether or not
there are more statements in the function.
HANDS-ON PRACTICE 14.8
In this Hands-On Practice you will create a form with inputs for name and age, and use
JavaScript to validate the data such that there will be data in the name field and an age
of 18 or greater. If there is nothing in the name field, an alert message will be displayed
instructing the user to enter a name. If the age entered is less than 18, an alert message
will be displayed instructing the user to enter an age of 18 or greater. If all data is valid,
an alert message will be displayed indicating that the data is valid and the form will be
submitted.
Let’s start by creating the form. Open a text editor and type the following. Notice that
theonsubmitform handler is embedded in the
onsubmit="return validateForm();">