Using Validation Routines
Số trang: 4
Loại file: pdf
Dung lượng: 21.42 KB
Lượt xem: 2
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
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.
Nội dung trích xuất từ tài liệu:
Using Validation Routines < Day Day Up >Using Validation RoutinesYou can think of a validation routine as a mini-scripting machine within your project thatvalidates the data it receives and then acts accordingly, based on whether that data isvalid or invalid. As such, most validation routines comprise functions composedprimarily of conditional (if) statements.Its typical to split validation routines into separate functions, one for each type of data tobe 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, asopposed to writing the validation routine repeatedly whenever a certain type of dataneeds to be validated.You can create two main types of validation routines (functions that validate data): • Those that dont receive parameters but work in a specific way • Those that receive parameters to provide additional functionality to your applicationLets take a look at each type.With a validation routine that is not sent parameters, you define the function to work in aspecific way, usually to validate a specific piece of data. Imagine you want to create aroutine 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 thats thefield defined in the script. Lets now look at how versatile we can make this function byallowing 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 trueor false value that indicates whether to look for an area code in the number to bevalidated, and a pNumber that represents the number to be validated. If lookForAreaCodeis true when called, the number (pNumber) sent to the function is valid only if it contains10 digits. If lookForAreaCode is false, the number sent to the function is valid only if itcontains 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_txttext field: That is a valid 10-digit telephone number.NOTEYoull remember from previous lessons that the values sent to a function can actually bevariables; therefore, you could use the validation function to validate the text in any textfield by referencing that fields name and text property (textField_txt.text) in the secondparameter of the function call.By creating a validation routine that accepts parameters and validates data accordingly,you increase the routines usefulness because you can employ it to validate similar data invarious ways.Conditional statements play an important role in validating data because that processentails nothing more than evaluating various conditions to determine whether user-inputdata is valid. The rules that define valid data are considered validation points. To definevalidation 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 someones 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 databased on multiple validation points. We will use and discuss each of these validationpoints in more detail in the exercises that follow. < Day Day Up >
Nội dung trích xuất từ tài liệu:
Using Validation Routines < Day Day Up >Using Validation RoutinesYou can think of a validation routine as a mini-scripting machine within your project thatvalidates the data it receives and then acts accordingly, based on whether that data isvalid or invalid. As such, most validation routines comprise functions composedprimarily of conditional (if) statements.Its typical to split validation routines into separate functions, one for each type of data tobe 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, asopposed to writing the validation routine repeatedly whenever a certain type of dataneeds to be validated.You can create two main types of validation routines (functions that validate data): • Those that dont receive parameters but work in a specific way • Those that receive parameters to provide additional functionality to your applicationLets take a look at each type.With a validation routine that is not sent parameters, you define the function to work in aspecific way, usually to validate a specific piece of data. Imagine you want to create aroutine 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 thats thefield defined in the script. Lets now look at how versatile we can make this function byallowing 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 trueor false value that indicates whether to look for an area code in the number to bevalidated, and a pNumber that represents the number to be validated. If lookForAreaCodeis true when called, the number (pNumber) sent to the function is valid only if it contains10 digits. If lookForAreaCode is false, the number sent to the function is valid only if itcontains 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_txttext field: That is a valid 10-digit telephone number.NOTEYoull remember from previous lessons that the values sent to a function can actually bevariables; therefore, you could use the validation function to validate the text in any textfield by referencing that fields name and text property (textField_txt.text) in the secondparameter of the function call.By creating a validation routine that accepts parameters and validates data accordingly,you increase the routines usefulness because you can employ it to validate similar data invarious ways.Conditional statements play an important role in validating data because that processentails nothing more than evaluating various conditions to determine whether user-inputdata is valid. The rules that define valid data are considered validation points. To definevalidation 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 someones 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 databased on multiple validation points. We will use and discuss each of these validationpoints in more detail in the exercises that follow. < Day Day Up >
Tìm kiếm theo từ khóa liên quan:
máy tính mạng máy tính internet phần mềm ứng dụng lập trình SQL HTML sever web XMLGợi ý tài liệu liên quan:
-
Giáo án Tin học lớp 9 (Trọn bộ cả năm)
149 trang 263 0 0 -
Ngân hàng câu hỏi trắc nghiệm môn mạng máy tính
99 trang 251 1 0 -
Giáo trình Hệ thống mạng máy tính CCNA (Tập 4): Phần 2
102 trang 244 0 0 -
Bài giảng: Lịch sử phát triển hệ thống mạng
118 trang 244 0 0 -
47 trang 237 3 0
-
Đề cương chi tiết học phần Thiết kế và cài đặt mạng
3 trang 234 0 0 -
80 trang 215 0 0
-
122 trang 212 0 0
-
Giáo trình Hệ thống mạng máy tính CCNA (Tập 4): Phần 1
122 trang 209 0 0 -
Giáo trình môn học/mô đun: Mạng máy tính (Ngành/nghề: Quản trị mạng máy tính) - Phần 1
68 trang 200 0 0