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):
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:
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. |