mx.formatters

# ZipCodeFormatter - AS3 Flex

Package | mx.formatters
Class | public class ZipCodeFormatter
Inheritance | ZipCodeFormatter Formatter Object

Language Version: | ActionScript 3.0

Product Version: | Flex 3

Runtime Versions: | Flash Player 9, AIR 1.1

The ZipCodeFormatter class formats a valid number into one of the following formats, based on a user-supplied `formatString` property.

- #####-####
- ##### ####
- #####
- ### ### (Canadian)

A six-digit number must be supplied for a six-digit mask. If you use a five-digit or a nine-digit mask, you can use either a five-digit or a nine-digit number for formatting.

If an error occurs, an empty String is returned and a String that describes the error is saved to the `error` property. The `error` property can have one of the following values:

- `"Invalid value"` means an invalid numeric value is passed to the `format()` method. The value should be a valid number in the form of a Number or a String, except for Canadian postal code, which allows alphanumeric values, or the number of digits does not match the allowed digits from the `formatString` property.
- `"Invalid format"` means any of the characters in the `formatString` property do not match the allowed characters specified in the `validFormatChars` property, or the number of numeric placeholders does not equal 9, 5, or 6.

MXML Syntax Show MXML Syntax
Hide MXML Syntax

The `<mx:ZipCodeFormatter>` tag inherits all of the tag attributes of its superclass, and adds the following tag attributes:

      <mx:ZipCodeFormatter
        formatString="#####|#####-####|### ###"
      />

View the examples

More examples

Formatter example
Writing an error handler function for an MX formatter
Formatting zip codes with an MX formatter

Learn more

Formatting Data
MX formatters

Related API Elements

mx.formatters.SwitchSymbolFormatter

Public Properties
Hide Inherited Public Properties
Show Inherited Public Properties

| Property | Defined By
--- | --- | ---
|  | constructor : Object A reference to the class object or constructor function for a given object instance. | Object
|  | error : String Description saved by the formatter when an error occurs. | Formatter
|  | formatString : String The mask pattern. | ZipCodeFormatter

Protected Properties
Hide Inherited Protected Properties
Show Inherited Protected Properties

| Property | Defined By
--- | --- | ---
|  | resourceManager : IResourceManager [read-only] A reference to the object which manages all of the application's localized resources. | Formatter

Public Methods
Hide Inherited Public Methods
Show Inherited Public Methods

| Method | Defined By
--- | --- | ---
|  | ZipCodeFormatter() Constructor. | ZipCodeFormatter
|  | format(value:Object):String [override] Formats the String by using the specified format. | ZipCodeFormatter
|  | hasOwnProperty(name:String):Boolean Indicates whether an object has a specified property defined. | Object
|  | isPrototypeOf(theClass:Object):Boolean Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter. | Object
|  | propertyIsEnumerable(name:String):Boolean Indicates whether the specified property exists and is enumerable. | Object
|  | setPropertyIsEnumerable(name:String, isEnum:Boolean = true):void Sets the availability of a dynamic property for loop operations. | Object
|  | toLocaleString():String Returns the string representation of this object, formatted according to locale-specific conventions. | Object
|  | toString():String Returns the string representation of the specified object. | Object
|  | valueOf():Object Returns the primitive value of the specified object. | Object

Protected Methods
Hide Inherited Protected Methods
Show Inherited Protected Methods

| Method | Defined By
--- | --- | ---
|  | resourcesChanged():void This method is called when a Formatter is constructed, and again whenever the ResourceManager dispatches a "change" Event to indicate that the localized resources have changed in some way. | Formatter

Property Detail

### formatString

property

`formatString:String`

Language Version: | ActionScript 3.0

Product Version: | Flex 3

Runtime Versions: | Flash Player 9, AIR 1.1

The mask pattern. Possible values are `"#####-####"`, `"##### ####"`, `"#####"`, `"###-###"` and `"### ###"`.

The default value is `"#####".`

Implementation
`public function get formatString():String`
`public function set formatString(value:String):void`
Constructor Detail

### ZipCodeFormatter

()
Constructor

`public function ZipCodeFormatter()`

Language Version: | ActionScript 3.0

Product Version: | Flex 3

Runtime Versions: | Flash Player 9, AIR 1.1

Constructor.

Method Detail

### format

()
method

`override public function format(value:Object):String`

Language Version: | ActionScript 3.0

Product Version: | Flex 3

Runtime Versions: | Flash Player 9, AIR 1.1

Formats the String by using the specified format. If the value cannot be formatted, return an empty String and write a description of the error to the `error` property.

Parameters

| `value:Object` — Value to format.

Returns

| `String` — Formatted String. Empty if an error occurs. A description of the error condition is written to the `error` property.

Examples How to use this example
ZipCodeFormatterExample.mxml
Copy

    <?xml version="1.0" encoding="utf-8"?>
    <!-- Simple example to demonstrate ZipCodeFormatter. -->
    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
            xmlns:s="library://ns.adobe.com/flex/spark"
            xmlns:mx="library://ns.adobe.com/flex/mx">

        <fx:Script>
            <![CDATA[
                import mx.events.ValidationResultEvent;

                private var vResult:ValidationResultEvent;

                // Event handler to validate and format input.
                private function Format():void {
                    vResult = zcVal.validate();

                    if (vResult.type == ValidationResultEvent.VALID) {
                        formattedZipcode.text = zipFormatter.format(zip.text);
                    } else {
                        formattedZipcode.text = "";
                    }
                }
            ]]>
        </fx:Script>

        <fx:Declarations>
            <mx:ZipCodeFormatter id="zipFormatter" formatString="#####-####"/>
            <mx:ZipCodeValidator id="zcVal" source="{zip}" property="text" allowedFormatChars=""/>
        </fx:Declarations>

        <s:Panel title="ZipCodeFormatter Example"
                width="75%" height="75%"
                horizontalCenter="0" verticalCenter="0">
            <mx:Form left="10" right="10" top="10" bottom="10">
                <mx:FormItem label="Enter a 5 or 9 digit U.S. ZIP code:" width="100%">
                    <s:TextInput id="zip" text=""/>
                </mx:FormItem>
                <mx:FormItem label="Formatted ZIP code: " width="100%">
                    <s:TextInput id="formattedZipcode" text="" editable="false"/>
                </mx:FormItem>
                <mx:FormItem>
                    <s:Button label="Validate and Format" click="Format();"/>
                </mx:FormItem>
            </mx:Form>
        </s:Panel>

    </s:Application>

© 2015 Adobe Systems Incorporated. All rights reserved.
Mon Apr 10 2017, 12:28 PM -07:00