tailieunhanh - Using Validation Routines

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. | 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 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 12 That is a valid 10-digit telephone number else if lookForAreaCode false 8 That