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:
function validateEmail() { if (email_ti.text.indexOf("@") < 2) { errors.push("@ missing in email or in the wrong place."); email_ti.setStyle("color", 0x990000); } if (email_ti.text.lastIndexOf(".") <= (email_ti.text.indexOf("@") + 2)) { errors.push(". missing in email or in the wrong place."); email_ti.setStyle("color", 0x990000); } if (email_ti.text.length < 8) { errors.push("Email address not long enough."); email_ti.setStyle("color", 0x990000); } }
The validateEmail()function validates the text entered into the email_ti instance and is made up of three conditional statements, each of which checks one of the individual validation points we outlined at the beginning of this exercise. Because these are separate if statements, rather than if/else, if/else if groupings, all of them will be evaluated. Let's look at each one in depth. The first statement reads as follows:
if (email_ti.text.indexOf("@") < 2) { errors.push("@ missing in email or in the wrong place."); email_ti.setStyle("color", 0x990000); }
You'll remember from Lesson 4, "Using Object Classes," that the indexOf() method returns the position (character number) where the value in the parentheses is first found in a string. Using this method, the statement above determines whether the first @ symbol appears before the third character in the email_ti instance. Because the first character in a string has an index number of 0, this statement evaluates to true and an error message is pushed into the errors array if the @ symbol is found at position 0 or 1. If the @ symbol doesn't occur anywhere within the email_ti instance, this statement returns a value of -1, which is still less than 2; this result causes the statement to evaluate to true and the error message to be pushed into the array. In addition to an error message being pushed into the errors 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.
TIP
Using the indexOf() method, you can also check for the existence of strings longer than one character. For example, you can check for http:// in a string by using something similar to the following syntax: string.indexOf("http://"). The number returned is the character number of the first letter in the string.
Let's look at the second statement in this function, which reads as follows:
if (email_ti.text.lastIndexOf(".") <= (email_ti.text.indexOf("@") + 2)) { errors.push(". missing in email or in the wrong place."); email_ti.setStyle("color", 0x990000); }
This statement uses the lastIndexOf() method similar to the indexOf() method except that it returns the character number of the last occurrence of the character in the parentheses. The following example returns 28:
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 relation to 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 errors array. 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 as follows:
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 is aa@bb.cc eight characters, including the @ symbol and the period. If the length of the text entered into the email_ti instance is fewer than eight characters, an error message 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.