Danh mục

Validating Sequences

Số trang: 7      Loại file: pdf      Dung lượng: 28.49 KB      Lượt xem: 14      Lượt tải: 0    
tailieu_vip

Phí tải xuống: 2,000 VND Tải xuống file đầy đủ (7 trang) 0
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Mặc dù các ký tự trong chuỗi có thể thay đổi, họ vẫn phải tuân theo quy tắc nhất định định dạng. Trình tự xác nhận là thường tham gia nhiều hơn một chút so với các loại dữ liệu xác nhận chủ yếu bởi vì có rất nhiều điểm để kiểm tra xác nhận.
Nội dung trích xuất từ tài liệu:
Validating Sequences < Day Day Up >Validating SequencesA sequence is a string of characters (letters, numbers, and special characters) placed in aspecific order or formatted in a special way. Following are some sample sequences: • Telephone number (xxx-xxxx) • Credit card number (xxxx xxxx xxxx xxxx) • Date (xx/xx/xxxx) • URL (http://www.xxxxxx.xxx)Although the characters within sequences may change, they must still follow certainformatting rules. Sequence validation is typically a bit more involved than other types ofdata validation primarily because there are numerous validation points to check. Bybreaking down the process, you can more readily understand how it works. The followingare some validation points for a typical email address: 1. It cannot contain more than one @ symbol. 2. It must include at least one period (separating the actual domain name from the domain extension, such as in mydomain.com or mydomain.net). 3. The last period must fall somewhere after the @ symbol, but it cant be either of the last two characters. 4. The @ symbol cannot be the first or second character. 5. There must be at least two characters between the @ symbol and the first period that precedes it. 6. The email address must include at least eight characters (aa@bb.cc).NOTEIf an email address has been checked for the six points listed above, youve performed areasonable validation. Although we could add many more validation points (for example,characters following the last period must be com, net, org, or something similar), yourcode would become extremely long and you would need to update it frequently to keeppace with changing Internet standards. Its crucial to determine the most importantvalidation points and check for them.In this exercise, well check only the following three validation points for text enteredinto the email_ti instance of our registration form: • The @ symbol must be included somewhere after the second character. • The email address must include a period at least two characters after the @ symbol. • The email address must consist of at least eight characters.TIPBe sure to provide users with clear instructions about how to format data. Its commonpractice to provide an example of correctly formatted data either above or below the textbox where its to be entered, providing a quick reference for the user. 1. Open validate3.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 validateEmail() { 5. 6. if (email_ti.text.indexOf(@) < 2) {7.8. errors.push(@ missing in email or in the wrong place.);9.10. email_ti.setStyle(color, 0x990000);11.12. }13.14. if (email_ti.text.lastIndexOf(.) Youll remember from Lesson 4, Using Object Classes, that the indexOf()method returns the position (character number) where the value in the parenthesesis first found in a string. Using this method, the statement above determineswhether the first @ symbol appears before the third character in the email_tiinstance. Because the first character in a string has an index number of 0, thisstatement evaluates to true and an error message is pushed into the errors array ifthe @ symbol is found at position 0 or 1. If the @ symbol doesnt occur anywherewithin the email_ti instance, this statement returns a value of -1, which is still lessthan 2; this result causes the statement to evaluate to true and the error message tobe pushed into the array. In addition to an error message being pushed into theerrors array whenever this error occurs, the text in the email_ti is styled as red,which is helpful for emphasizing the location of the error.TIPUsing the indexOf() method, you can also check for the existence of strings longerthan one character. For example, you can check for http:// in a string by usingsomething similar to the following syntax: string.indexOf(http://). The numberreturned is the character number of the first letter in the string.Lets look at the second statement in this function, which reads as follows:if (email_ti.text.lastIndexOf(.) email_ti.text = derek.franklin@derekfranklin.com, email_ti.text.lastIndexOf(.)Using this method, the statement looks at the position of the last period in relationto the @ symbol. If the period is less than two characters to the right of the @symbol, this statement proves true and an error message is pushed into the errorsarray. Again, if this error occurs, the text in the email_ti instance is styled in red,indicating the location of the error.The third statement in this function is the easiest one to comprehend. It reads asfollows:if (email_ti.text.length < 8) { errors.push(Email address not long enough.); email_ti.setStyle(color, 0x990000);}As mentioned earlier in this lesson, the smallest reasonable email address isaa@bb.cc—eight characters, including the @ symbol and the period. If the lengthof the text entered into the email_ti instance is fewer than eight characters, an errormessage stating the email address is too short is pushed into the error array and the text in the email_ti instance is styled as red. After all of these statements have been evaluated, you may find that the information entered into the email_ti instance is invalid on all counts. In this case, three different error messages would be pushed into the errors array.3. Add the following function call just below the validateName() function call in the validateForm() function definition:4.5. validateEmail();6. This is a call to the function we just defined. Placing this function call here adds e ...

Tài liệu được xem nhiều: