With the Actions panel still open, add this function definition at the end of the current script:
function validateForm () { errorLog.text = ""; errors.length = 0; validateName(); if (errors.length > 0) { errorLog.htmlText = "<b>These errors were found:</b><br>"; var i = -1; while (++i < errors.length) { errorLog.htmlText += errors[i] + newline; } } else { gotoAndStop ("Confirm", 1); } }
In the previous step, we created a validation function to check the data entered into the name text field. As we progress through this lesson, we'll create several more validation functions to validate the data entered into our other form fields. However, the function you'll create in this step validateForm() is really the mother of all these other functions because eventually it will be used to call all of the individual validation functions and then finalize the validation process, including outputting error messages to the errorLog text field. Take special note of the sequence of actions in this function: This flow plays an important role in how the function works.
The first two actions in this function clear the errorLog text field as well as any messages that may exist in the errors array. Obviously, the first time the form is validated, these actions are worthless because both begin empty anyway. Any subsequent validation of the entered data will require that the errorLog text field begin the process empty of any text and that any error messages in the errors array be erased.
The next line contains a function call to the validateName() function we defined in the previous step. This will cause that function to execute and thus validate the data in the name text field. As a result, an error message is either pushed into the errors array (if data is invalid) or it is not.
NOTE
We will add more function calls here as we define them in the following exercises.
The next action in this function is an if statement, which is evaluated only after the validateName() function has completed its job. This is where the sequence of actions becomes important. If an error message gets pushed into the errors array as a result of calling the validateName() function, the length property of the errors array gets changed to 1, indicating that it contains at least one error message. This if statement then looks at the length property of the errors array and acts accordingly. If the length property has a value greater than 0 (indicating error messages within the array), the resulting actions output those messages to the errorLog text field. If errors.length is 0, this means there are no error messages and the data is valid; thus, a gotoAndStop() action sends the timeline to the scene named Confirm.
The actions used to output the error messages are pretty straightforward: The first creates the line that appears initially. Using a couple of HTML bold tags (<b></b> ), the text "These errors were found:" is displayed in bold, followed by a line break (<br> ). Next, a looping statement is used to loop through the errors array to add the error messages stored there to the errorLog text field (using ActionScript you're by now familiar with). When this process is complete, the errorLog text field will display any messages within the errors array.
Although you're by now familiar with the text property of text fields, you'll notice that the above script uses htmlText in relation to the errorLog text field. You can use this text field property in two ways (similar to the text property): to retrieve the HTML representation of text displayed in a text field, or to set the text displayed (rendered in HTML) in a text field. To better understand this, take a look at the following example:
message.text = "<b>Hello there!</b>";
The above script will display <b>Hello there!</b> in a text field named message. Because we're setting the text property, the displayed text is an exact representation of the string value. In contrast, take a look at the following:
message.htmlText = "<b>Hello there!</b>";
The above script will display Hello there! in the text field. In the above, we simply changed the script so that it sets the text to be rendered in HTML, as it should be. When setting HTML text in a text field using the htmlText setting, you're telling the field to be aware that the incoming text has HTML tags and thus should be rendered as such.
A text field must be HTML-aware to properly render a string of text containing HTML tags. You can indicate that a text field is an HTML text field by pressing the "Render text as HTML" button in the Property inspector, or by setting the text field's html property to true . For our project, we simply selected the option (for the errorLog text field) on the Property inspector.