Chapter 14. Validation, Formatting, and Regular Expressions

Validation, formatting, and regular expressions may seem a somewhat strange grouping at first glance, but they tend to be used for similar things in the everyday experience of developers: parsing the format of strings to detect a certain pattern, altering strings into a certain format if specific patterns are or are not encountered, and returning error messages to users if necessary properties are not encountered. That is, all three are useful for dealing with the sorts of data that we need from third parties or users that may not always be supplied in the format required by our applications—things like phone numbers, capitalized names, currencies, zip codes, and ISBN numbers. The Flex Framework provides two powerful tools to integrate this type of parsing and formatting with the UI elements of the Framework in the Validator and Formatter classes. Beneath both of these is the regular expression or RegExp object introduced in ActionScript 3. This is a venerable and powerful programming tool, used by nearly all, and loved and loathed in equal measure for its incredible power and difficult syntax.

The Validator is an event dispatcher object that checks a field within any Flex control to ensure that the value submitted falls within its set parameters. These parameters can indicate a certain format, whether a field is required, or the length of a field. Validation can be implemented simply by setting the source property of the ...

Get Flex 4 Cookbook 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.