VALIDATING NUMBERS


Validating numbers is not much different than validating strings, which we've already discussed.

In this exercise, we'll create one last validation function to validate the data entered into the zip text field. As a valid U.S. Zip code, the data entered must meet two requirements:

  • Length. It must include exactly five characters.

  • Type. It must contain numbers; text is invalid

TIP

When validating numbers, you may need to call for the number entered to be more or less in value than another number which by now you should be able to do easily!


  1. Open validate5.fla in the Lesson13/Assets folder.

    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 this function definition at the end of the current script:

     function validateZip () {    if (zip.text.length != 5 || isNaN(zip.text) == true) {      errors.push("Please enter a valid zip.");    }  } 

    When called, this function checks that the data entered into the zip text field meets two conditions regarding length and type the validation points we defined at the beginning of this exercise. The conditional statement here states that if the zip text field does not contain five characters, or if it consists of text (rather than numbers), the following text string should be pushed into the errors array: "Please enter a valid zip."

    NOTE

    To refresh your understanding of isNaN() , review the information in the Validating Strings exercise.

  3. Add the following function call just below the validateState() function call:

     validateZip(); 

    graphics/13fig16.gif

    This is a call to the function we just defined. Placing this function call here adds Zip code validation capability to the main validateForm() function. This function call is placed just above the 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 this statement is evaluated. In the end, if the validateName() , validateEmail(), validateState(), or validateZip() functions find errors, their corresponding messages will be displayed in the errorLog text field.

  4. Choose Control > Test Movie to test the project up to this point.

    Enter an invalid Zip code in the zip text field to see what the validation process turns up. Pressing the Clear button resets the visual and data elements in the scene.

  5. Close the test movie to return to the authoring environment and save this file as validate6.fla.

    This completes the validation portion of this lesson. The great thing about the way we've set up this project's validation process is that it's dynamic. To analyze additional rules, or validation points, for any of the text fields, all you have to do is add more conditional statements to the appropriate function. The validation process can grow as your needs grow.

    If the validation process determines that all of the user-entered data is valid, the timeline is sent to the Confirm scene. This scene then displays a custom message based on the validated data and to add some style to it, we'll take advantage of Flash's ability to format text using HTML.



Macromedia Flash MX ActionScripting Advanced. Training from the Source
Macromedia Flash MX ActionScripting: Advanced Training from the Source
ISBN: 0201770229
EAN: 2147483647
Year: 2002
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net