Previous Example-|-Next Example-|-Return to Chapter Listing

Example 12.2:
onchange: Form Validation


This time we're using the onchange event handler. Change the value in the box, then click outside. You'll see an Alert box whatever you change the number to.
Type your age (you must be 18 or over):



These are the scripts we used. First, in the HEAD: <SCRIPT LANGUAGE="JavaScript"> <!-- function testAge(form) { var nAge = form.ageBox.value; if (nAge >= 18) { if (nAge >=100) { alert("You entered an age of " + nAge +". Are you sure this is correct?") } else { alert("You entered an age of " + nAge) } } else { alert("You entered an age of " + nAge + ". You must be 18 or over to use this service!") } } //--> </SCRIPT> Then we created this form element: <FORM> Type your age (you must be 18 or over): <BR> <INPUT TYPE="text" NAME="ageBox" onchange="testAge(this.form)"> </FORM>
Previous Example-|-Next Example-|-Return to Chapter Listing