Validating Strings
As mentioned earlier in this lesson, when validating different types of data (names, phone numbers, email addresses, and so on) it's best to break the process into specialized functions or validation routines. We will begin that process in this exercise.The first type of data our form asks for is the user's name. For our form, the data entered must 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 name begin with a D, but the ones we have chosen are sufficient for our purposes.In this exercise, we'll create a function for validating the name entered into our project's registration form.
Open validate2.fla.We'll build on the project from the last exercise.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 validateName() {
if (name_ti.text.length < 2 || isNaN(name_ti.text) == false) {
errors.push("Please enter a valid name.");
name_ti.setStyle("color", 0x990000);
}
}


In Step 2, we created a validation function to check the data entered into the name_ti instance. As we progress through this lesson, we'll create several more validation functions to validate the data entered into our other TextInput instances. The function created in this stepvalidateForm()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 is invalid.
function validateForm() {
errorLog_lb.removeAll();
errors.length = 0;
validateName();
if (errors.length > 0) {
errorLog_lb.defaultIcon = "errorIcon";
var altColorArray:Array = new Array(0xF9F2F2, 0xECD9D9);
errorLog_lb.alternatingRowColors = altColorArray;
errorLog_lb.rollOverColor = 0xFFFFFF;
errorLog_lb.selectionColor = 0xFFFFFF;
errorLog_lb.dataProvider = errors;
} else {
gotoAndStop ("Confirm");
}
}
NOTEWe will add more function calls as we define them in the following exercises.The next action in this function is an if statement, which is evaluated only after the validateName() function has completed its job. This is where the sequence of actions becomes important. If an error message is pushed into the errors array as a result of calling the validateName() function, the length property of the errors array is changed to 1, indicating that it contains at least one error message. This if statement then looks at the length property of the errors array and acts accordingly. If the length property has a value greater than 0 (indicating error messages within the array), the resulting actions output those messages to the errorLog_lb List component instance. If errors.length is 0, this means there are no error messages and the data is valid; therefore, a gotoAndStop() action sends the timeline to the frame labeled Confirm.


This script calls the validateForm() function when the submit_pb instance is clicked, causing the actions within that function to be executed when the button is released.Choose Control > Test Movie to test the project up to this point.Enter an invalid name into the name_ti instance to see what the validation process turns up. Click the Clear button to reset the scene's visual and data elements.Close the test movie to return to the authoring environment, and save this file as validate3.fla.We'll build on this file in the following exercise.
submit_pb.addEventListener("click", validateForm);