Notifying the User with Alert and Confirm Dialog Boxes

If the user enters an incorrect value for one of the PERS components, the form should display a dialog box informing the user of the error. To accomplish this, you can use the alert() method. The alert() method operates in the same way as the prompt() method, except that is does not provide an input box in which the user can type a response. Instead, it simply displays a dialog box containing a message. The syntax for the alert() method method is:


where Message is the message that will appear in the dialog box.

JavaScript provides another method called the confirm() method, which works in the same way as the prompt() and alert() methods. The confirm method displays a message in a dialog box that is similar to the alert dialog box, except that is includes both an OK button and a Cancel button. If the user clicks the OK button, the value "true" is returned. If the user clicks the Cancel button, the value returned is "false." In your program, you need to use the alert() method. You'll replace the comment line in the PERS() function with a command to alert the user that only values of 0, 1, 2, 3, 4, or 5 are allowed.

In the PERS() function, replace the comment "//alert the user" with the following line of code:

alert("You must enter a 0, 1, 2, 3, 4, or 5");

Save your changes, and then reopen the file in your Web browser. To test the revised PERS() function, you'll attempt to enter an incorrect value in the Bold field. Click the Bold input box and change its value from the default of 0 to 5, and then press the Tab key. Your Web browser should display a dialog box. Click the OK button. Your cursor is returned to the Bold input box. Note that you cannot tab out of this box or click outside it without receiving the error message. Change the value in the Bold input box to 5, and then press the Tab key. The cursor advances to the Crunchy input box. Attempt to enter values greater than 5 in the other component input boxes and verify that the alert dialog box opens when you do so.

Congratulations, you have become a master at completing forms!!!!