Integrating Flash with Web Applications > Creating forms > Verifying entered data
Verifying entered data
For a form that passes variables to an application on a Web server, you'll want to verify that users are entering proper information. For example, you don't want users to enter text in a phone number field. Use a series of set variable
actions in conjunction with for
and if
to evaluate entered data.
The following sample action checks to see whether the entered data is a number, and that the number is in the format ###-###-####. If the data is valid, the message "Good, this is a valid phone number!" is displayed. If the data is not valid, the message "This phone number is invalid!" is displayed.
To use this script in a movie, create two text fields on the Stage and choose Input in the Text Options panel for each. Assign the variable phoneNumber
to one text field and assign the variable message
to the other. Attach the following action to a button on the Stage next to the text fields:
on (release) { valid = validPhoneNumber(phoneNumber); if (valid) { message = "Good, this is a valid phone number!"; } else { message = "This phone number is invalid!"; } function isdigit(ch) { return ch.length == 1 && ch >= '0' && ch <= '9'; } function validPhoneNumber(phoneNumber) { if (phoneNumber.length != 12) { return false; } for (var index = 0; index < 12; index++) { var ch = phoneNumber.charAt(index); if (index == 3 || index == 7) { if (ch != "-") { return false; } } else if (!isdigit(ch)) { return false; } } return true; } }
To send the data, create a button that has an action similar to the following: (Replace the getURL
arguments with arguments appropriate for your movie.)
on (release) { if (valid) { getURL("http://www.webserver.com", "_self", "GET"); } }
For more information about these ActionScript statements, see set
, for
, and if
in Chapter 7, "ActionScript Dictionary."