Follow Flex After Dark on Twitter

Form Validation

When allowing users (damn humans) to input data it is often desired (or necessary) to validate the entered data for correctness.

Flex provides several Validators that can be used with Forms to ensure entered user-entered data is correctly formatted.

Triggering Validation

Common triggers and events:

  • TextInput / focusOut - triggers when the field loses focus
  • TextInput / change - triggers when the value of the field changes
  • Button / click - triggers when a button is clicked

Form Validation Example

   <!-- Validators -->
   <mx:StringValidator source="{txtFirstName} property="text"
      maxLength="100"
      trigger="{btnOK}" triggerEvent="click"
   />
   
   <mx:StringValidator source="{txtLastName} property="text"
      maxLength="100"
      trigger="{btnOK}" triggerEvent="click"
   />
   
   <mx:EmailValidator source="{txtEmailAddress}" property="text" required="false"
      trigger="{txtEmailAddress}" triggerEvent="focusOut"
   />
   
   
   <!-- Form -->
   <mx:Form id="frmContact">
      
      <mx:FormHeading label="Contact Information"/>
      
      <mx:FormItem label="First Name:" required="true">
         <mx:TextInput id="txtFirstName"/>
      </mx:FormItem>
      
      <mx:FormItem label="Last Name:" required="true">
         <mx:TextInput id="txtLastName"/>
      </mx:FormItem>
      
      <mx:FormItem label="Email Address:" required="false">
         <mx:TextInput id="txtEmailAddress"/>
      </mx:FormItem>
      
      <mx:FormItem label="Phone Number:" required="false">
         <mx:TextInput id="txtEmailAddress"/>
      </mx:FormItem>
      
      <mx:FormItem>
         <mx:Button id="btnOK" label="OK"/>
      </mx:FormItem>
      
   </mx:Form>

More information on Validators...

Related Links:

Related Docs
  • Flex Forms Forms in Flex can be used for collecting, and validating, user input.
  • Flex Validators Flex Validators are used for validating user-input data.
Recent Docs
Random Docs
  • Flex Projects Overview of the different types of projects that can be created with Flex Builder.
  • Flex Builder Overview Overview of FlexBuilder, Adobe's integrated development environment (IDE) for Flex development.
  • Flex Fonts Working with Fonts in Flex, including embedding and runtime loading.
  • Flex DataGrid The Flex DataGrid control is used for displaying tabular data.
  • Flex Builder Help Overview of Flex Builder's help system.