Previous Chapter's Examples-|-Next Example-|-Return to Chapter Listing

Example 12.1:
onblur: Form Validation


This form uses the onblur event handler to verify the form entry. Note, however, that if you enter text instead of a number, you'll get an error message. We'll see how to fix that later.
Type in an age of 17 or less, then click outside the text box to see what will happen. Then try an age of 100 or more.
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 + ". 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" onblur="testAge(this.form)"> </FORM>
Previous Chapter's Examples-|-Next Example-|-Return to Chapter Listing