Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
More products
Solutions
Digital marketing
Digital media
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Buy
Home use for personal and home office
Education for students, educators, and staff
Business for small and medium businesses
Licensing programs for businesses, schools, and government
Special offers
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
My products and services
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out My orders My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
Purchase requires verification of academic eligibility
Subtotal
Review and Checkout
Adobe Developer Connection / Flex Developer Center / Flex in a Week /

Exercise 3.5: Using formatters

by Trilemetry

Trilemetry
  • Trilemetry, Inc.

Content

  • Create a DateTimeFormatter instance
  • Use the DateTimeFormatter instance to format the selected date
  • Create a PhoneFormatter instance
  • Use the PhoneFormatter instance to format the mobile phone field

Modified

2 May 2011

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
Flash Builder Flex RIA

Requirements

Prerequisite knowledge

Exercise 1.1: Setting up Flash Builder and your project files

User level

Beginning

Required products

  • Flash Builder 4.5 Premium (Download trial)

Exercise files

  • ex3_05_starter.zip
  • ex3_05_solution.zip

In this exercise you will use the Flex DateTimeFormatter component to format the selected date from a calendar as shown in Figure 1. You will also learn how to format a phone number.

Figure 1. Review your task for this exercise.
Figure 1. Review your task for this exercise.

In this exercise, you will learn how to:

  • Create a DateTimeFormatter instance
  • Use the DateTimeFormatter instance to format the selected date
  • Create a PhoneFormatter instance
  • Use the PhoneFormatter instance to format the mobile phone field

Create a DateTimeFormatter instance

In this section you will create a DateTimeFormatter instance that will be used to format the date of the time entry.

  1. Download the ex3_05_starter.zip file provided in the Exercise files section and extract the ex3_05_starter.fxp to your computer.
  2. Open Flash Builder.
  3. Import the ex3_05_starter.fxp file.
  4. From the Package Explorer view, components package, open the VehicleRequestForm.mxml file.
  5. Run the application.
  6. Select a Pickup Date.

    Note the format of the date you selected in the Alert window (see Figure 2).

Note the date format in the Alert window.
Figure 2. Note the date format in the Alert window.
  1. Return to the VehicleRequestForm.mxml file in Flash Builder.
  2. Locate the Declarations block.
<fx:Declarations> </fx:Declarations>
  1. Within the Declarations block, create a DateTimeFormatter instance.
<fx:Declarations> <s:DateTimeFormatter/> </fx:Declarations>
  1. To the DateTimeFormatter instance, add the id property with a value of requestDateFormatter and the dateTimePattern property with a value of MM-dd-yyyy.
<s:DateTimeFormatter id="requestDateFormatter" dateTimePattern="MM-dd-yyyy"/>
  1. Save the file.

Use the DateTimeFormatter instance to format the selected date

In this section you will use the DateTimeFormatter instance format the date displayed in the Alert window.

  1. Within the Script block, locate the dateChangeHandler() function.
  2. Within the dateChangeHandler() function, to the Alert.show() method located outside the if statement, use the requestDateFormatter.format() method to format the event.result.selectedDate value.
protected function dateChangeHandler(event:CalendarLayoutChangeEvent):void { Alert.show('You have selected ' + requestDateFormatter.format(event.target.selectedDate)); ... }
  1. Save the file.
  2. Run the application.
  3. Select a Pickup Date.

The date in the Alert window is formatted as specified by the DateTimeFormatter object (see Figure 3).

Select a date to view the Alert message and see the formatted date.
Figure 3. Select a date to view the Alert message and see the formatted date.

Create a PhoneFormatter instance

In this section you will create a PhoneFormatter instance that will be used to format the Office Phone and Mobile Phone fields of the Employee Portal Vehicle Request form.

  1. Return to the VehicleRequestForm.mxml file in Flash Builder.
  2. Within the Declarations block, below the DateTimeFormatter instance, create a PhoneFormatter instance.
<fx:Declarations> <s:DateTimeFormatter id="requestDateFormatter" formatString="MM-dd-yyyy"/> <mx:PhoneFormatter /> </fx:Declarations>
  1. To the PhoneFormatter instance, add the id property with a value of phoneFieldFormatter, the formatString property assigned to (###) ###-####.
<mx:PhoneFormatter id="phoneFieldFormatter" formatString="(###) ###-####"/>
  1. Save the file.

Use the PhoneFormatter instance to format the mobile phone field

In this section you will use the PhoneFormatter instance to format the mobile phone after a number has been inserted.

  1. Within the Form container, locate the FormItem container that nests the TextInput control with an id property of mobilePhone.
  2. For the TextInput control, use the content assist tool to generate the focusOut event (see Figure 4).
Generate the focusOut event.
Figure 4. Generate the focusOut event.
  1. Use the content assist tool to generate a handler function for the focusOut event (see Figure 5).
Generate a handler function for the focusOut event.
Figure 5. Generate a handler function for the focusOut event.
  1. Within the Script block, locate the mobilePhone_focusOutHandler() function and delete the generated stub code.
  2. Within the function, for the mobilePhone.text value, use the phoneFieldFormatter.format() method to format the number entered in the mobilePhone field.
protected function mobilePhone_focusOutHandler(event:FocusEvent):void { mobilePhone.text = phoneFieldFormatter.format(mobilePhone.text); }
  1. Save the file.
  2. Run the application.
  3. In the Mobile Phone field, type 5555555555.
  4. Click the DateChooser below the Mobile Phone field to remove focus from the field.
  5. The mobile phone number you entered is formatted as shown in Figure 6.

Figure 6. Select a date to view the Alert message and see the formatted date.
Figure 6. Select a date to view the Alert message and see the formatted date.
  1. Delete the text from Mobile Phone field and type 555.
  2. Click the DateChooser to remove focus from the field.
  3. Note that this time the text you entered is deleted from the field. The formatter returns a blank field when the input is invalid. The only string that is valid is a 10 digit number without any characters.

    Now you will add a custom validation check to return an Alert message if the input to the formatter is invalid.

  4. Return to Flash Builder.
  5. Locate the mobilePhone_focusOutHandler() function.
  6. In the function and below the mobilePhone formatter, type if and press Ctrl+Space twice to show a list of code templates. Select the template for the if statement.
  7. Check to see if the text entered in the Mobile Phone field is invalid by adding phoneFieldFormatter.error as the condition of the statement.
  8. Within the conditional statement, use the Alert.show() method to display the text Phone format error: followed by the error by using phoneFieldFormatter.error.
if(phoneFieldFormatter.error) { Alert.show("Phone format error: " + phoneFieldFormatter.error); }
  1. Save the file and run the application.
  2. Type 555 in the Mobile Phone field and click the DataChooser to remove focus.
  3. Note that the formatter still returns a blank field because the number you entered was invalid, but an alert message also appears indicating an invalid value (see Figure 7).

Figure 7. An alert message showing that the number entered into the text field was an invalid value.
Figure 7. An alert message showing that the number entered into the text field was an invalid value.

In this tutorial you learned how to use a DateFormatter class to display a date selected from a DateChooser control. You also learned how to use the PhoneFormatter class. In the next exercise you will use Flex framework validation classes to validate a form.

More Like This

  • Exercise 5.5: Creating and applying skins
  • Exercise 4.9: Animating components with effects
  • Exercise 2.9: Use the Data Services wizard to connect to a service
  • Exercise 3.4: Passing data to the server with the HTTPService class
  • Exercise 3.1: Creating an event and dispatching the event object
  • Exercise 2.3: Using the addEventListener() method
  • Exercise 2.6: Separating the model, view, and controller
  • Exercise 4.5: Creating and using item renderers and item editors
  • Exercise 4.8: Creating a scalable user interface
  • Exercise 4.1: Passing data to item renderers for display

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps
  • Student and Teacher Editions

Solutions

  • Digital marketing
  • Digital media
  • Web Experience Management

Industries

  • Education
  • Financial services
  • Government

Help

  • Product help centers
  • Orders and returns
  • Downloading and installing
  • My Adobe

Learning

  • Adobe Developer Connection
  • Adobe TV
  • Training and certification
  • Forums
  • Design Center

Ways to buy

  • For personal and home office
  • For students, educators, and staff
  • For small and medium businesses
  • For businesses, schools, and government
  • Special offers

Downloads

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Security
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy Policy and Cookies (Updated)

Ad Choices

Reviewed by TRUSTe: site privacy statement