Using Validation Routines

Chia sẻ: Qweqwdasd Qweqdasda | Ngày: | Loại File: PDF | Số trang:4

0
36
lượt xem
5
download

Using Validation Routines

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Sử dụng thói quen Xác Nhận Bạn có thể nghĩ ra một xác nhận thường xuyên như một máy mini-kịch bản trong dự án của bạn mà xác nhận các dữ liệu mà nó nhận được và sau đó hành vi phù hợp, dựa trên dữ liệu cho dù đó là hợp lệ hay không hợp lệ. Như vậy, thói quen xác nhận hầu hết các chức năng bao gồm gồm chủ yếu là có điều kiện (nếu có) báo cáo.

Chủ đề:
Lưu

Nội dung Text: Using Validation Routines

  1. < 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:
  2. 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
  3. 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,
  4. 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 >
Đồng bộ tài khoản