Validating Entries

The flashGuestbook component features entry validation to enable you to ensure the correct fields have data entered before entries are submitted. If the field is specied as 'email' or 'website', the email address and URL will be checked for format validity.

 

Setting up the guestBook component

1. First, if you have not already done so you will need to complete the Basic Tutorial. Once you have, open the .fla file you created.

 

 

2. Select the flashGuestbook that's on the stage and press Alt+F7 to open the component inspector.

 

 

3. Click on the value next to the 'GuestBook Entry Fields' parameter and click on the magnifying glass to open the dialog box.

 

 

4. Click on the arrow next to the 'name_ti' entry. Set the value next to 'isValidate' to true. For the value of the 'validationMessage', enter a message to display if a user forgets to enter their name. For example "Please enter your name".

 

 

5. Click on the arrow next to the 'message_ta' entry. Set 'isValidate' to true. Enter a message to display if the customer hasn't entered a message, "Please enter a message" for example.

 

 

Adding the Alert component

6. Press Ctrl+L (Win) or Cmnd+L (Mac) to open the library panel. Press Ctrl+F7 (Win) or Cmnd+F7 (Mac) to open the components panel, locate the Alert component in the User Interface folder and drag a copy into the library.

 

Note that if you already own it, you can use the Bit Component Set Alert instead of the User Interface Alert that shipped with Flash.

 

 

7. Select the keyframe of the 'Actions' layer and press Alt+F9 to open the Actions panel. Beneath the code already entered type the following script:

import mx.controls.Alert;

 

This code imports the Alert class. Below that, enter the following code:

 

var validationListener:Object = new Object();

 

This code creates a new object named 'validationListener'. Next add the following:

 

validationListener.validationError = function(err:Object){
		Alert.show(err.validationMessage, "guestBook alert!");
}

 

This code sets up a function to respond to the guestBook's 'validationError' event with its first line. The second line uses the Alert component's 'show' method to create an alert window when the function is called. Finally add the following line:

 

myGuestBook.addEventListener("validationError", validationListener);

 

This code registers the 'validationListener' object with the guestBook instance to be notified when the 'validationError' event is triggered.

 

 

8. Press Ctrl+S (Win) or Cmnd+S (Mac) to save your file and Shift+F12 to publish it.

 

Upload the .html and .swf files created when you published the movie to the same directory as your 'script' folder and open the .html file in a browser to test your file.

 

Attempt to submit a message without entering a name or message to see the alert appear.