Book HomeActionScript: The Definitive GuideSearch this book

18.7. Empty Text Fields and the for-in Statement

To check all the values of the variables on a timeline, we can use the for-in statement as described in Chapter 6, "Statements". Undefined text fields (those that appear on screen but contain the undefined value), however, are not enumerated by the for-in statement. (Fields containing the empty string ("") or only spaces are not considered empty and are therefore enumerated.)

The invisibility of undefined text fields in for-in loops can cause problems for error-checking scripts. Scripts that use a for-in loop to cycle through a series of text fields must be written to account for undefined text fields. For example, here we attempt to check a movie clip called formClip to see if any of its variables contain the empty string:

for (i in formClip) {
  if (formClip[i] == "") {
    trace(i + " is empty! don't submit the form!");
    break;
  }
}

As is, that code would not function as desired because undefined text fields would not be enumerated by the loop and would never be checked. To force an undefined text field to be enumerated in a for-in loop, we must deliberately assign the empty string to a corresponding timeline variable. For example, we would attach this script to a frame of formClip in order to fix our previous example:

// Assign our text fields the empty string so that
// they show up in our for-in loop
formField1 = "";
formField2 = "";


Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.