Validating Strings
Số trang: 8
Loại file: pdf
Dung lượng: 26.77 KB
Lượt xem: 9
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:
Như đã đề cập trước đó trong bài học này, khi xác nhận các loại dữ liệu (tên, số điện thoại, địa chỉ email, vv) tốt nhất để phá vỡ quá trình vào chức năng chuyên ngành hoặc thói quen xác nhận. Chúng tôi sẽ bắt đầu quá trình trong tập thể dục này. Loại đầu tiên của mẫu dữ liệu của chúng tôi yêu cầu là tên của người dùng. Đối với hình thức của chúng tôi, các dữ liệu nhập vào phải đáp ứng hai yêu cầu...
Nội dung trích xuất từ tài liệu:
Validating Strings < Day Day Up >Validating StringsAs mentioned earlier in this lesson, when validating different types of data (names, phonenumbers, email addresses, and so on) its best to break the process into specializedfunctions or validation routines. We will begin that process in this exercise.The first type of data our form asks for is the users name. For our form, the data enteredmust meet two requirements: • Length. The name must be at least two characters long. • Type. Text must be used; a number cannot be accepted as a valid name.NOTEWe could define other validation points, such as a maximum length or even that the namebegin with a D, but the ones we have chosen are sufficient for our purposes.In this exercise, well create a function for validating the name entered into our projectsregistration form. 1. Open validate2.fla. Well build 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 validateName() { 5. 6. if (name_ti.text.length < 2 || isNaN(name_ti.text) == false) { 7. 8. errors.push(Please enter a valid name.);9.10. name_ti.setStyle(color, 0x990000);11.12. }13.14. }15. When called, this function checks the data entered in the name_ti instance for two conditions: length and type (the validation points we defined at the beginning of this exercise). The conditional statement here says that if the name_ti instance contains fewer than two characters or is a number (which would be considered an error), the string Please enter a valid name should be pushed into the errors array. In addition, we use the setStyle() method to change the color of text in the name_ti instance to red to make the location of the error a little more noticeable. Although weve used the length property in other scripts, some of this syntax is new and thus requires explanation. One of the validation points weve defined for the name_ti instance is that a number entered into it would be considered an error (of course, a name containing a number, such as derek66, would be acceptable). To determine whether data is anumber or text string, we would use the isNaN() function. This built-in functionverifies that the argument passed to it (within the parentheses) is not a number(hence the name isNaN, or is Not a Number). Take a look at the followingexamples:isNaN(Box) returns a value of true because the value of Box is a string,meaning its true that its not a number.isNaN(465) returns a value of false because 465 is a number—that is, its false tostate that 465 is not a number.Thus, isNaN(name_ti.text) == false is the same as saying, If what name_ticontains is a number…If either of the conditions in the statement exists, data has been input improperly.As a result, the actions within the if statement are executed. The first action withinthe statement uses the push() method of the Array object to add an element to theend of the errors array. (This element is whats enclosed in the parentheses.) If youthink of the errors array as a book, using the push() method is like adding a page tothe end of the book, with the string within the parentheses representing the text onthat page. The page number of this page would be its index position within thearray. In the case of this function, if the data in the name_ti instance is invalid, the text Please enter a valid name is pushed into the errors array. Next, well create a function to retrieve this error from the array (if name_ti doesnt contain a valid entry and an error is pushed into the array) and display it in the errorLog_lb List component instance.3. With the Actions panel still open, add the following function definition at the end of the current script:4.5. function validateForm() {6.7. errorLog_lb.removeAll();8.9. errors.length = 0;10.11. validateName();12.13. if (errors.length > 0) {14.15. errorLog_lb.defaultIcon = errorIcon;16.17. var altColorArray:Array = new Array(0xF9F2F2, 0xECD9D9);18.19. errorLog_lb.alternatingRowColors = altColorArray;20.21. errorLog_lb.rollOverColor = 0xFFFFFF;22.23. errorLog_lb.selectionColor = 0xFFFFFF;24.25. errorLog_lb.dataProvider = errors;26.27. } else {28.29. gotoAndStop (Confirm);30.31. }32.33. }34. In Step 2, we created a validation function to check the data entered into the name_ti instance. As we progress through this lesson, well create several more validation functions to validate the data entered into our other TextInput instances. The function created in this step—validateForm()—is really the mother of all these other functions; eventually it will be used to call all the individual validation functions and then finalize the validation process, including outputting error messages to the errorLog_lb List component instance. Take special note of the sequence of actions in this function: this flow plays an important role in how the function works. The first two actions in this function clear the errorLog_lb instance of any displayed errors as well as any messages that may exist in the errors array. Obviously, the first time the form is validated, these actions are worthless because both begin empty. Any subsequent validation of the entered data will require that the errorLog_lb instance begin the validation process empty of any displayed items, and that any error messages in the errors array be erased. The next line contains a function call to the validateName() function we defined in Step 2. This will cause that function to execute and validate the data in the name_ti instance. As a result, an error message is pushed into the errors array if data isinvalid ...
Nội dung trích xuất từ tài liệu:
Validating Strings < Day Day Up >Validating StringsAs mentioned earlier in this lesson, when validating different types of data (names, phonenumbers, email addresses, and so on) its best to break the process into specializedfunctions or validation routines. We will begin that process in this exercise.The first type of data our form asks for is the users name. For our form, the data enteredmust meet two requirements: • Length. The name must be at least two characters long. • Type. Text must be used; a number cannot be accepted as a valid name.NOTEWe could define other validation points, such as a maximum length or even that the namebegin with a D, but the ones we have chosen are sufficient for our purposes.In this exercise, well create a function for validating the name entered into our projectsregistration form. 1. Open validate2.fla. Well build 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 validateName() { 5. 6. if (name_ti.text.length < 2 || isNaN(name_ti.text) == false) { 7. 8. errors.push(Please enter a valid name.);9.10. name_ti.setStyle(color, 0x990000);11.12. }13.14. }15. When called, this function checks the data entered in the name_ti instance for two conditions: length and type (the validation points we defined at the beginning of this exercise). The conditional statement here says that if the name_ti instance contains fewer than two characters or is a number (which would be considered an error), the string Please enter a valid name should be pushed into the errors array. In addition, we use the setStyle() method to change the color of text in the name_ti instance to red to make the location of the error a little more noticeable. Although weve used the length property in other scripts, some of this syntax is new and thus requires explanation. One of the validation points weve defined for the name_ti instance is that a number entered into it would be considered an error (of course, a name containing a number, such as derek66, would be acceptable). To determine whether data is anumber or text string, we would use the isNaN() function. This built-in functionverifies that the argument passed to it (within the parentheses) is not a number(hence the name isNaN, or is Not a Number). Take a look at the followingexamples:isNaN(Box) returns a value of true because the value of Box is a string,meaning its true that its not a number.isNaN(465) returns a value of false because 465 is a number—that is, its false tostate that 465 is not a number.Thus, isNaN(name_ti.text) == false is the same as saying, If what name_ticontains is a number…If either of the conditions in the statement exists, data has been input improperly.As a result, the actions within the if statement are executed. The first action withinthe statement uses the push() method of the Array object to add an element to theend of the errors array. (This element is whats enclosed in the parentheses.) If youthink of the errors array as a book, using the push() method is like adding a page tothe end of the book, with the string within the parentheses representing the text onthat page. The page number of this page would be its index position within thearray. In the case of this function, if the data in the name_ti instance is invalid, the text Please enter a valid name is pushed into the errors array. Next, well create a function to retrieve this error from the array (if name_ti doesnt contain a valid entry and an error is pushed into the array) and display it in the errorLog_lb List component instance.3. With the Actions panel still open, add the following function definition at the end of the current script:4.5. function validateForm() {6.7. errorLog_lb.removeAll();8.9. errors.length = 0;10.11. validateName();12.13. if (errors.length > 0) {14.15. errorLog_lb.defaultIcon = errorIcon;16.17. var altColorArray:Array = new Array(0xF9F2F2, 0xECD9D9);18.19. errorLog_lb.alternatingRowColors = altColorArray;20.21. errorLog_lb.rollOverColor = 0xFFFFFF;22.23. errorLog_lb.selectionColor = 0xFFFFFF;24.25. errorLog_lb.dataProvider = errors;26.27. } else {28.29. gotoAndStop (Confirm);30.31. }32.33. }34. In Step 2, we created a validation function to check the data entered into the name_ti instance. As we progress through this lesson, well create several more validation functions to validate the data entered into our other TextInput instances. The function created in this step—validateForm()—is really the mother of all these other functions; eventually it will be used to call all the individual validation functions and then finalize the validation process, including outputting error messages to the errorLog_lb List component instance. Take special note of the sequence of actions in this function: this flow plays an important role in how the function works. The first two actions in this function clear the errorLog_lb instance of any displayed errors as well as any messages that may exist in the errors array. Obviously, the first time the form is validated, these actions are worthless because both begin empty. Any subsequent validation of the entered data will require that the errorLog_lb instance begin the validation process empty of any displayed items, and that any error messages in the errors array be erased. The next line contains a function call to the validateName() function we defined in Step 2. This will cause that function to execute and validate the data in the name_ti instance. As a result, an error message is pushed into the errors array if data isinvalid ...
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 216 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 210 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 201 0 0