Validating Against a List of Choices
Số trang: 4
Loại file: pdf
Dung lượng: 13.07 KB
Lượt xem: 3
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:
Xác nhận đối với một Danh sách các lựa chọn Có lần khi một giá trị nhập vào một biểu mẫu phải phù hợp với một trong nhiều lựa chọn. Ví dụ, nếu hình thành một yêu cầu người dùng nhập vào một cụ thể màu đỏ, vàng, hoặc màu xanh-và người dùng vô tình đi vào que hoặc yullow thay vào đó, điều quan trọng để có thể phát hiện lỗi khi xác nhận hình thức.
Nội dung trích xuất từ tài liệu:
Validating Against a List of Choices < Day Day Up >Validating Against a List of ChoicesThere are times when a value entered into a form must match one of several choices. Forexample, if a form asks the user to enter a specific color—red, yellow, or blue—and theuser accidentally enters rod or yullow instead, its important to be able to detect that errorwhen validating the form.If youre going to compare data entered against a list of choices, obviously you mustdefine that list first. In ActionScript, an array can include a list of choices. The validationprocess is simply a matter of comparing the entered data against the values in the array tosee if theres a match.In this exercise, well create another validation routine—this one to compare whatsentered in the state_ti instance against an array of state names.TIPIts best to have users do as little manual data entry as possible. Instead of requiring theuser to input data that matches one of several choices, you would be better off providinga drop-down list from which the user could choose a value—a method that eliminates theneed for data validation. In some applications, however, this is impossible—for example,in a quiz application that contains a list of answers you dont want users to be able toaccess. In such cases, theres no way to avoid manual validation. 1. Open validate4.fla. We will continue building on the project from the last exercise.2. With the Actions panel open, select Frame 1 on the Actions layer and add the following function definition at the end of the current script:3.4. function validateState() {5.6. var states:Array = [California, Indiana, North Carolina, Oklahoma];7.8. var matchFound:Boolean = false;9.10. for (var i = 0; i The next several lines in this function are part of a looping statement, which is used to loop through all the values in the states array, comparing each to the value entered in the state_ti instance. If a match is found, matchFound is set to true. If no match is found, the value of this variable remains false (its initial state), indicating an error. The last part of the function contains an if statement thats executed after the looping statement has completed its job. It says that if matchFound is false (which it will be if no match is found), an appropriate error message should be pushed into the errors array and the state_ti instances text should be styled as red (as in the other functions weve created thus far).3. Add the following function call just below the validateEmail() function call in the validateForm() function definition:4.5. validateState();6. This is a call to the validateState() function we just defined. Placing this function call here adds state-name validation capability to the main validateForm() function. This function call is placed just above the conditional statement that checks the length of the errors array because that function is able to push error messages into the array, thus affecting its length and the way the statement is evaluated. In the end, if validateName(), validateEmail(), or validateState() finds errors, the corresponding messages will be displayed in the errorLog_lb List component instance.4. Choose Control > Test Movie to test your project thus far. Enter an invalid state name (anything other than the four state names in the array) into the state_ti instance to see what the validation process turns up. Click the Clear button to reset the visual and data elements in the scene.5. Close the test movie to return to the authoring environment, and save this file as validate5.fla. We will build on this file in the following exercise. < Day Day Up >
Nội dung trích xuất từ tài liệu:
Validating Against a List of Choices < Day Day Up >Validating Against a List of ChoicesThere are times when a value entered into a form must match one of several choices. Forexample, if a form asks the user to enter a specific color—red, yellow, or blue—and theuser accidentally enters rod or yullow instead, its important to be able to detect that errorwhen validating the form.If youre going to compare data entered against a list of choices, obviously you mustdefine that list first. In ActionScript, an array can include a list of choices. The validationprocess is simply a matter of comparing the entered data against the values in the array tosee if theres a match.In this exercise, well create another validation routine—this one to compare whatsentered in the state_ti instance against an array of state names.TIPIts best to have users do as little manual data entry as possible. Instead of requiring theuser to input data that matches one of several choices, you would be better off providinga drop-down list from which the user could choose a value—a method that eliminates theneed for data validation. In some applications, however, this is impossible—for example,in a quiz application that contains a list of answers you dont want users to be able toaccess. In such cases, theres no way to avoid manual validation. 1. Open validate4.fla. We will continue building on the project from the last exercise.2. With the Actions panel open, select Frame 1 on the Actions layer and add the following function definition at the end of the current script:3.4. function validateState() {5.6. var states:Array = [California, Indiana, North Carolina, Oklahoma];7.8. var matchFound:Boolean = false;9.10. for (var i = 0; i The next several lines in this function are part of a looping statement, which is used to loop through all the values in the states array, comparing each to the value entered in the state_ti instance. If a match is found, matchFound is set to true. If no match is found, the value of this variable remains false (its initial state), indicating an error. The last part of the function contains an if statement thats executed after the looping statement has completed its job. It says that if matchFound is false (which it will be if no match is found), an appropriate error message should be pushed into the errors array and the state_ti instances text should be styled as red (as in the other functions weve created thus far).3. Add the following function call just below the validateEmail() function call in the validateForm() function definition:4.5. validateState();6. This is a call to the validateState() function we just defined. Placing this function call here adds state-name validation capability to the main validateForm() function. This function call is placed just above the conditional statement that checks the length of the errors array because that function is able to push error messages into the array, thus affecting its length and the way the statement is evaluated. In the end, if validateName(), validateEmail(), or validateState() finds errors, the corresponding messages will be displayed in the errorLog_lb List component instance.4. Choose Control > Test Movie to test your project thus far. Enter an invalid state name (anything other than the four state names in the array) into the state_ti instance to see what the validation process turns up. Click the Clear button to reset the visual and data elements in the scene.5. Close the test movie to return to the authoring environment, and save this file as validate5.fla. We will build on this file in the following exercise. < 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 XMLTài liệu liên quan:
-
Giáo án Tin học lớp 9 (Trọn bộ cả năm)
149 trang 278 0 0 -
Bài giảng: Lịch sử phát triển hệ thống mạng
118 trang 258 0 0 -
Ngân hàng câu hỏi trắc nghiệm môn mạng máy tính
99 trang 257 1 0 -
Giáo trình Hệ thống mạng máy tính CCNA (Tập 4): Phần 2
102 trang 256 0 0 -
47 trang 242 3 0
-
Đề cương chi tiết học phần Thiết kế và cài đặt mạng
3 trang 240 0 0 -
80 trang 228 0 0
-
Giáo trình Hệ thống mạng máy tính CCNA (Tập 4): Phần 1
122 trang 218 0 0 -
122 trang 217 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 213 0 0