With the Actions panel open, select Frame 1 on the Actions layer and add the following function definition at the end of the current script:
function validateState() {
var states:Array = ["California", "Indiana", "North Carolina", "Oklahoma"];
var matchFound:Boolean = false;
for (var i = 0; i <= states.length; ++i) {
if (state_ti.text == states[i]) {
matchFound = true;
break;
}
}
if (!matchFound) {
errors.push("Please enter a valid state.");
state_ti.setStyle("color", 0x990000);
}
}
The
validateState() function validates the data entered into the
state_ti instance.
The first action in this function creates an array named
states, which will hold all the possible choices. To keep this as short as possible, we included only four state names, although you could easily add all 50.
The next action creates a variable named
matchFound and assigns it an initial value of
false. The importance of this variable will become evident in a moment.
The next several lines in this function are part of a looping statement, which is used to loop through all the values in the
states array, comparing each to the value entered in
the
state_ti instance. If a match is found,
matchFound is set to
true. If no match is found, the value of this variable remains
false (its initial state), indicating an error.

The last part of the function contains an
if statement that's executed after the looping statement has completed its job. It says that if
matchFound is
false (which it will be if no match is found), an appropriate error message should be pushed into the
errors array, and the
state_ti instance's text should be styled as red (as in the other functions we created thus far).