Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 14. Validation, Formatting, and Regular Expressions

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 Validator to the control where the user input will occur and indicating the property that the Validator should check. If a validation error occurs, the Validator will dispatch the error event to the control, and the control will display a custom error message that has been set in the Validator. There are many predefined validators in the Flex Framework (e.g., for credit cards, phone numbers, email, and social security numbers), but this chapter focuses primarily on building custom validators and integrating validators and validation events into controls.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial