mx.formatters

# PhoneFormatter - AS3 Flex

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

Language Version: | ActionScript 3.0

Product Version: | Flex 3

Runtime Versions: | Flash Player 9, AIR 1.1

The PhoneFormatter class formats a valid number into a phone number format, including international configurations.

A shortcut is provided for the United States seven-digit format. If the `areaCode` property contains a value and you use the seven-digit format string, (###-####), a seven-digit value to format automatically adds the area code to the returned String. The default format for the area code is (###). You can change this using the `areaCodeFormat` property. You can format the area code any way you want as long as it contains three number placeholders.

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, or the value contains a different number of digits than what is specified in the format String.
- `"Invalid format"` means any of the characters in the `formatString` property do not match the allowed characters specified in the `validPatternChars` property, or the `areaCodeFormat` property is specified but does not contain exactly three numeric placeholders.

MXML Syntax Show MXML Syntax
Hide MXML Syntax

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

      <mx:PhoneFormatter
        areaCode="-1"
        areaCodeFormat="(###)"
        formatString="(###) ###-####"
        validPatternChars="+()#-. "
      />

View the examples

More examples

Formatter example
Writing an error handler function for an MX formatter
Formatting phone numbers 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
--- | --- | ---
|  | areaCode : Object Area code number added to a seven-digit United States format phone number to form a 10-digit phone number. | PhoneFormatter
|  | areaCodeFormat : String Default format for the area code when the areacode property is rendered by a seven-digit format. | PhoneFormatter
|  | 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 String that contains mask characters that represent a specified phone number format. | PhoneFormatter
|  | validPatternChars : String List of valid characters that can be used in the formatString property. | PhoneFormatter

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
--- | --- | ---
|  | PhoneFormatter() Constructor. | PhoneFormatter
|  | format(value:Object):String [override] Formats the String as a phone number. | PhoneFormatter
|  | 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

### areaCode

property

`areaCode:Object`

Language Version: | ActionScript 3.0

Product Version: | Flex 3

Runtime Versions: | Flash Player 9, AIR 1.1

Area code number added to a seven-digit United States format phone number to form a 10-digit phone number. A value of `-1` means do not prepend the area code.

The default value is `-1.`

Implementation
`public function get areaCode():Object`
`public function set areaCode(value:Object):void`

### areaCodeFormat

property

`areaCodeFormat:String`

Language Version: | ActionScript 3.0

Product Version: | Flex 3

Runtime Versions: | Flash Player 9, AIR 1.1

Default format for the area code when the `areacode` property is rendered by a seven-digit format.

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

Implementation
`public function get areaCodeFormat():String`
`public function set areaCodeFormat(value:String):void`

### formatString

property

`formatString:String`

Language Version: | ActionScript 3.0

Product Version: | Flex 3

Runtime Versions: | Flash Player 9, AIR 1.1

String that contains mask characters that represent a specified phone number format.

The default value is `"(###) ###-####".`

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

### validPatternChars

property

`validPatternChars:String`

Language Version: | ActionScript 3.0

Product Version: | Flex 3

Runtime Versions: | Flash Player 9, AIR 1.1

List of valid characters that can be used in the `formatString` property. This property is used during validation of the `formatString` property.

The default value is `"+()#- .".`

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

### PhoneFormatter

()
Constructor

`public function PhoneFormatter()`

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 as a phone number. 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
PhoneFormatterExample.mxml
Copy

    <?xml version="1.0" encoding="utf-8"?>
    <!-- Simple example to demonstrate PhoneFormatter. -->
    <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 = pnVal.validate();
                    if (vResult.type == ValidationResultEvent.VALID) {
                        formattedPhone.text = phoneFormatter.format(phone.text);
                    } else {
                        formattedPhone.text = "";
                    }
                }
            ]]>
        </fx:Script>

        <fx:Declarations>
            <mx:PhoneFormatter id="phoneFormatter"
                    formatString="(###) ###-####" validPatternChars="#-() "/>

            <mx:PhoneNumberValidator id="pnVal" source="{phone}" property="text"
                    allowedFormatChars=""/>
        </fx:Declarations>

        <s:Panel title="PhoneFormatter Example"
                width="75%" height="75%"
                horizontalCenter="0" verticalCenter="0">
            <mx:Form left="10" right="10" top="10" bottom="10">
                <mx:FormItem label="Enter a 10-digit phone number:">
                    <s:TextInput id="phone" text="" width="75%"/>
                </mx:FormItem>
                <mx:FormItem label="Formatted phone number: ">
                    <s:TextInput id="formattedPhone" text="" width="75%" 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