Macromedia Flash MX ActionScripting: Advanced Training from the Source

You can think of a validation routine as a mini-scripting machine within your project that validates the data it receives and then acts accordingly (based on whether that data is valid or invalid). As such, most validation routines comprise functions composed primarily of conditional (if ) statements.

It's typical to split validation routines into separate functions, one for each type of data to be validated (for example, one function for validating strings, another for numbers, and so on). This allows you to script a function once and then use it anywhere in your project, as opposed to writing the validation routine repeatedly whenever a certain type of data needs to be validated.

You can create two main types of validation routines (functions that validate data):

  • Those that do not receive parameters but work in a specific way

  • Those that receive parameters to provide additional functionality to your application

Let's take a look at each.

With a validation routine that is not sent parameters, you define the function to work in a specific way usually to validate a specific piece of data. Say you wanted to create a routine to validate a seven-digit telephone number (which includes nine characters in all: seven digits and two hash [-] marks) that the user enters into a text field named telephone. The structure of that function would look like the following:

function validateTelephone () { if (telephone.length == 9) { // number is valid, so do these specific actions } else { // number is invalid, so do these specific actions } }

This routine can only validate the data in the telephone text field because that's the field that's been defined in the script. Let's now look at how versatile we can make this function by allowing it to accept a couple of parameters:

function validateTelephone (lookForAreaCode, pNumber) if (lookForAreaCode == true && pNumber.length == 12) { message.text = "That is a valid 10-digit telephone number"; } else if (lookForAreaCode == false && pNumber.length == 9){ message.text = "That is a valid 7-digit telephone number"; } else { message = "That is not a valid telephone number"; } }

When called, this validation function receives two parameters: lookForAreaCode , a true or false value that indicates whether or not to look for an area code in the number to be validated, and pNumber , which represents the number to be validated. If lookForAreaCode is true when called, the number (pNumber ) sent to the function is valid only if it contains 10 numbers. If lookForAreaCode is false , the number sent to the function is valid only if it contains 7 numbers.

A call to this validation routine would look like the following:

validateTelephone(true, 812-555-1234);

After processing this call, the function would display the following in the message text field: "That is a valid 10-digit telephone number."

TIP

You'll remember from previous lessons that the values sent to a function can actually be variables. Thus, you could use the above function to validate the text in any text field by referencing that field's name and text property (textField.text ) in the second parameter of the function call.

By creating a validation routine that accepts parameters and validates data accordingly, you increase the routine's usefulness because you can employ it to validate similar data in various ways.

Conditional statements play an important role in validating data since that process really entails nothing more than evaluating various conditions to determine whether user-input data is valid. The rules that define valid data are considered validation points. To define validation points, you must consider the following:

  • Length. Does the data contain the correct number of characters? A typical U.S. Zip code, for example, contains five digits. Thus, if a user-entered Zip code includes less than five characters, this is an error. Or take a name: Because most names include more than one character, you would have an error if the length of the data entered were one. (A length of zero means nothing has been entered. If you require something to be entered, this would be an error.)

  • Value. Is the value of the data entered more, less, or equal to that which is considered valid? If you were asking for someone's age (which you should never do), you might define a lower limit of 18 and an upper limit as 100. If the value entered were more or less than either of these, this would be an error.

  • Type. Is the data entered a number when it should be a string, or vice-versa? When asking for someone's weight (which, again, you should never do), pizza would be an error because a number is required.

  • Sequence. Is the data properly formatted? Some data needs to contain numbers, letters, and other characters, all placed in a specific sequence for example, phone numbers (123-4567), dates (01/23/45), account numbers (1-2345-67-890), and so on. Missing or misplaced dashes, slashes, or other characters represent errors.

Most validation routines contain conditional statements that are used to validate data based on multiple validation points. We will be using and discussing each of these validation points in more detail in the exercises that follow.

Категории