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
  • Flex Form Validation Form Validation techniques and usage examples.
  • ActionScript Array The ActionScript Array class and usage examples.
  • ActionScript Timer Using the ActionScript Timer class.
  • Flex Data Providers Several Flex components, including all list-based controls, display data from a data provider, a collection or object that contains the data displayed by the control.
  • Flex Item Renderers A Flex Item Renderer is a component used by data-driven controls, including lists, for displaying each item of a data provider.
Random Docs
  • Flex Item Renderers A Flex Item Renderer is a component used by data-driven controls, including lists, for displaying each item of a data provider.
  • Flex Builder Help Overview of Flex Builder's help system.
  • Flex HTTP Service Flex provides an HTTP Service in its RPC (Remoting) package for standard HTTP operations.
  • ActionScript Language Introduction to the ActionScript 3.0 programming language.
  • ActionScript Functions ActionScript Functions are first-class objects and can be registered as event listeners.