Chapter 13. Validating and Formatting Data

When working with data, you’ll frequently need to ensure that the data adheres to certain rules. When the data is from user input, this is called validation. When the data is being displayed or needs to be in a particular form before storing it or sending it to a service method, this is called formatting. The Flex framework provides mechanisms for both of these types of operations. In this chapter, we will look at both validating and formatting data.

Validating User Input

When you work with user input, you frequently may want to validate that input before submitting it for some sort of course of action, either client-side or server-side. For example, if you want to submit form data to a server script that inserts the data to a database, you may need to verify that the values are in the correct format so that you can insert them (e.g., a numeric value needs to be a number, and a date needs to be formatted so that your script can parse it into the format you require for the database).

You can write your own validation procedures for this purpose. Yet most validations require rewriting the same basic code time after time. All that redundancy leads to a lot of time spent rewriting the same basic code rather than focusing on new tasks. For this reason, the Flex framework ships with a type of component called a validator, which you can use to assist with validating user input. There are a handful of standard validator types, including StringValidator ...

Get Programming Flex 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.