| Package | mx.formatters |
| Class | public class PhoneFormatter |
| Inheritance | PhoneFormatter Formatter Object |
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.
Hide MXML SyntaxThe <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="+()#-. "
/>
See also
| 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 | |
![]() | defaultInvalidFormatError : String [static] Error message for an invalid format string specified to the formatter. | Formatter | |
![]() | defaultInvalidValueError : String [static] Error messages for an invalid value specified to the formatter. | Formatter | |
![]() | 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 | ||
![]() | prototype : Object [static] A reference to the prototype object of a class or function object. | Object | |
| validPatternChars : String List of valid characters that can be used
in the formatString property. | PhoneFormatter | ||
| Method | Defined By | ||
|---|---|---|---|
Constructor. | PhoneFormatter | ||
Formats the String as a phone number. | PhoneFormatter | ||
![]() | Indicates whether an object has a specified property defined. | Object | |
![]() | Indicates whether an instance of the Object class is in the prototype chain of the object specified
as the parameter. | Object | |
![]() | Indicates whether the specified property exists and is enumerable. | Object | |
![]() | Sets the availability of a dynamic property for loop operations. | Object | |
![]() | Returns the string representation of the specified object. | Object | |
![]() | Returns the primitive value of the specified object. | Object | |
| areaCode | property |
areaCode:Object [read-write]
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.
public function get areaCode():Object public function set areaCode(value:Object):void| areaCodeFormat | property |
areaCodeFormat:String [read-write]
Default format for the area code when the
areacode
property is rendered by a seven-digit format.
The default value is "(###) ".
public function get areaCodeFormat():String public function set areaCodeFormat(value:String):void| formatString | property |
formatString:String [read-write] String that contains mask characters that represent a specified phone number format.
The default value is "(###) ###-####".
public function get formatString():String public function set formatString(value:String):void| validPatternChars | property |
validPatternChars:String [read-write]
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 "+()#- .".
public function get validPatternChars():String public function set validPatternChars(value:String):void| PhoneFormatter | () | Constructor |
public function PhoneFormatter()Constructor.
| format | () | method |
override public function format(value:Object):String
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. |
String —
Formatted String. Empty if an error occurs. A description
of the error condition is written to the
error
property.
|
<?xml version="1.0" encoding="utf-8"?>
<!-- Simple example to demonstrate PhoneFormatter. -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx: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= "";
}
}
]]>
</mx:Script>
<mx:PhoneFormatter id="phoneFormatter"
formatString="(###) ###-####" validPatternChars="#-() "/>
<mx:PhoneNumberValidator id="pnVal" source="{phone}" property="text"
allowedFormatChars=""/>
<mx:Panel title="PhoneFormatter Example" width="75%" height="75%"
paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom="10">
<mx:Form>
<mx:FormItem label="Enter a 10-digit phone number:">
<mx:TextInput id="phone" text="" width="75%"/>
</mx:FormItem>
<mx:FormItem label="Formatted phone number: ">
<mx:TextInput id="formattedPhone" text="" width="75%" editable="false"/>
</mx:FormItem>
<mx:FormItem>
<mx:Button label="Validate and Format" click="Format();"/>
</mx:FormItem>
</mx:Form>
</mx:Panel>
</mx:Application>