1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Using Validation Routines docx

4 273 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 4
Dung lượng 21,42 KB

Nội dung

< Day Day Up > Using Validation Routines 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, such as 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 don't 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 type. 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. Imagine you want to create a routine to validate a seven-digit telephone number that includes eight characters in all— seven digits and one hyphen (-) that the user enters into a text field named telephone_txt. The structure of that function would look similar to the following: function validateTelephone () { if (telephone_txt.length == 8) { // 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_txt text field because that's the field 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:Boolean, pNumber:String) if (lookForAreaCode == true && pNumber.length == 12) { message_txt.text = "That is a valid 10-digit telephone number"; } else if (lookForAreaCode == false && pNumber.length == 8){ message_txt.text = "That is a valid 7-digit telephone number"; } else { message_txt.text = "That is not a valid telephone number"; } } When called, this validation function receives two parameters: lookForAreaCode, a true or false value that indicates whether to look for an area code in the number to be validated, and a pNumber that 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 digits. If lookForAreaCode is false, the number sent to the function is valid only if it contains seven digits (and a hyphen). A call to this validation routine would look similar to the following: validateTelephone(true, 812-555-1234); After processing this call, the function would display the following in the message_txt text field: "That is a valid 10-digit telephone number." NOTE You'll remember from previous lessons that the values sent to a function can actually be variables; therefore, you could use the validation function to validate the text in any text field by referencing that field's name and text property (textField_txt.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 because that process 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. If a user-entered zip code includes fewer than five digits, this is an error. Or imagine a name: because most names include more than one character, you would have an error if the length of the data entered were 1. (A length of 0 means nothing has been entered. If you require something to be entered, this would be an error.) • Value. Is the value of the entered data more, less, or equal to what is considered valid? If you were asking for someone's age, you might define a lower limit of 18 and an upper limit of 100. If the value entered were more than 100 or less than 18, this would be an error. • Type. Is the data entered a number when it should be a string, or vice versa? If the user specifies a garment size on an order, "pizza" would be an error when 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 hyphens, 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 use and discuss each of these validation points in more detail in the exercises that follow. < Day Day Up > . < Day Day Up > Using Validation Routines You can think of a validation routine as a mini-scripting machine within. such, most validation routines comprise functions composed primarily of conditional (if) statements. It's typical to split validation routines into

Ngày đăng: 24/12/2013, 07:17

TỪ KHÓA LIÊN QUAN

w