| 包 | mx.formatters |
| 类 | public class ZipCodeFormatter |
| 继承 | ZipCodeFormatter Formatter Object |
formatString 属性将有效数字设置为下列格式之一。
必须为六位数字的号码提供一个六位数字的掩码。如果您使用五位数字或九位数字的掩码,则可以设置五位数字或九位数字号码的格式。
如果出现错误,则会返回一个空 String,以及一个说明已将此错误保存到 error 属性的 String。error 属性可以是下列值之一:
"Invalid value" 表示传递给 format() 方法的数值无效。此值应该是 Number 或 String 形式的有效数字(适用于字母数字值,加拿大邮政编码除外),或者数字位数不符合 formatString 属性中允许的位数。
"Invalid format" 表示 formatString 属性中的字符总数不符合 validFormatChars 属性中指定的允许字符数,或者数字占位符数目不等于 9、5 或 6。
隐藏 MXML 语法The <mx:ZipCodeFormatter> tag
inherits all of the tag attributes of its superclass,
and adds the following tag attributes:
<mx:ZipCodeFormatter
formatString="#####|#####-####|### ###"
/>
另请参见
| 属性 | 定义方 | ||
|---|---|---|---|
![]() | constructor : Object
对类对象或给定对象实例的构造函数的引用。 | Object | |
![]() | defaultInvalidFormatError : String [静态] 为 formatter 指定的格式字符串无效时出现的错误消息。 | Formatter | |
![]() | defaultInvalidValueError : String [静态] 为 formatter 指定的值无效时出现的错误消息。 | Formatter | |
![]() | error : String 发生错误时由 formatter 保存的说明。 | Formatter | |
| formatString : String 掩码模式。 | ZipCodeFormatter | ||
![]() | prototype : Object [静态]
对类或函数对象的原型对象的引用。 | Object | |
| 方法 | 定义方 | ||
|---|---|---|---|
构造函数。 | ZipCodeFormatter | ||
使用指定的格式设置 String 的格式。 | ZipCodeFormatter | ||
![]() |
指示对象是否已经定义了指定的属性。 | Object | |
![]() |
指示 Object 类的实例是否在指定为参数的对象的原型链中。 | Object | |
![]() |
指示指定的属性是否存在、是否可枚举。 | Object | |
![]() |
设置循环操作动态属性的可用性。 | Object | |
![]() |
返回指定对象的字符串表示形式。 | Object | |
![]() |
返回指定对象的原始值。 | Object | |
| formatString | 属性 |
formatString:String [读写]
掩码模式。可能的值为 "#####-####"、"##### ####"、"#####"、"###-###" 和 "### ###"。
默认值为 "#####".
public function get formatString():String public function set formatString(value:String):void| ZipCodeFormatter | () | 构造函数 |
public function ZipCodeFormatter()构造函数。
| format | () | 方法 |
override public function format(value:Object):String
使用指定的格式设置 String 的格式。如果无法设置值的格式,则会返回一个空 String,并将对错误的描述写入 error 属性。
参数
value:Object — 要设置格式的值。 |
String —
格式化的 String。如果发生错误,则为空字符串。将有关错误条件的描述写入 error 属性。
|
<?xml version="1.0" encoding="utf-8"?>
<!-- Simple example to demonstrate ZipCodeFormatter. -->
<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 = zcVal.validate();
if (vResult.type==ValidationResultEvent.VALID) {
formattedZipcode.text= zipFormatter.format(zip.text);
}
else {
formattedZipcode.text= "";
}
}
]]>
</mx:Script>
<mx:ZipCodeFormatter id="zipFormatter" formatString="#####-####"/>
<mx:ZipCodeValidator id="zcVal" source="{zip}" property="text" allowedFormatChars=""/>
<mx:Panel title="ZipCodeFormatter Example" width="75%" height="75%"
paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom="10">
<mx:Form width="100%">
<mx:FormItem label="Enter a 5 or 9 digit U.S. ZIP code:" width="100%">
<mx:TextInput id="zip" text=""/>
</mx:FormItem>
<mx:FormItem label="Formatted ZIP code: " width="100%">
<mx:TextInput id="formattedZipcode" text="" editable="false"/>
</mx:FormItem>
<mx:FormItem>
<mx:Button label="Validate and Format" click="Format();"/>
</mx:FormItem>
</mx:Form>
</mx:Panel>
</mx:Application>