格式化
SwitchSymbolFormatter 类
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="format()" backgroundColor="#FFFFFF">
<mx:Script>
<![CDATA[
import mx.formatters.SwitchSymbolFormatter;
private function format():void {
// Create Instance of the SwitchSymbolFormatter
var switchSymbolFormatter:SwitchSymbolFormatter = new SwitchSymbolFormatter();
// Apply formatter on the unformatted TextInput data using selectedItem from ComboBox
formatted.text = switchSymbolFormatter.formatValue(formatString.selectedItem.toString(), unformatted.text);
}
]]>
</mx:Script>
<mx:Panel title="SwitchSymbolFormatter Example" width="400" height="200"
paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom="10">
<mx:Form>
<mx:FormItem label="Unformatted Data:">
<mx:TextInput id="unformatted" text="123456789" editable="false"/>
</mx:FormItem>
<mx:FormItem label="Format String:">
<mx:ComboBox id="formatString" change="format()">
<mx:ArrayCollection>
<mx:String>#-########</mx:String>
<mx:String>###-######</mx:String>
<mx:String>##-###-####</mx:String>
<mx:String>#######-##</mx:String>
</mx:ArrayCollection>
</mx:ComboBox>
</mx:FormItem>
<mx:FormItem label="Formatted Data:">
<mx:TextInput id="formatted" editable="false"/>
</mx:FormItem>
</mx:Form>
</mx:Panel>
</mx:Application>
如果要格式化的字符串中包含“#”,可用
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="format()" backgroundColor="#FFFFFF">
<mx:Script>
<![CDATA[
import mx.formatters.SwitchSymbolFormatter;
private function format():void {
// Create Instance of the SwitchSymbolFormatter and pass in mask character
var switchSymbolFormatter:SwitchSymbolFormatter = new SwitchSymbolFormatter("*");
// Apply formatter on the unformatted TextInput data using selectedItem from ComboBox
formatted.text = switchSymbolFormatter.formatValue(formatString.selectedItem..toString(), unformatted.text);
}
]]>
</mx:Script>
<mx:Panel title="SwitchSymbolFormatter Example" width="400" height="200"
paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom="10">
<mx:Form>
<mx:FormItem label="Unformatted Data:">
<mx:TextInput id="unformatted" text="123456789" editable="false"/>
</mx:FormItem>
<mx:FormItem label="Format String:">
<mx:ComboBox id="formatString" change="format()">
<mx:ArrayCollection>
<mx:String># *-********</mx:String>
<mx:String># ***-******</mx:String>
<mx:String># **-***-****</mx:String>
<mx:String># *******-**</mx:String>
</mx:ArrayCollection>
</mx:ComboBox>
</mx:FormItem>
<mx:FormItem label="Formatted Data:">
<mx:TextInput id="formatted" editable="false"/>
</mx:FormItem>
</mx:Form>
</mx:Panel>
</mx:Application>
自定义格式化组件需要扩展mx.formatters.Formatter,并且重写format方法
如果发生格式化错误,会给error属性返回一个字符串信息提示
package
{
import mx.formatters.Formatter;
// Custom formatters must extend mx.formatters.Formatter
public class ReverseFormatter extends Formatter {
public function ReverseFormatter() {
super();
}
// Custom formatters must override format().
override public function format(formatObj:Object):String {
if(formatObj.length == 0) {
// return empty string and set error property if string has zero length.
error="Can not format an empty String";
return ""
} else {
error=null;
var returnString:String = "";
// loop through value and build string in reverse
for(var i:Number=formatObj.length; i>=0; i--){
returnString = returnString + formatObj.charAt(i);
}
return returnString;
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
backgroundColor="#FFFFFF" width="475" height="175" xmlns:comps="*">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
private function format():void{
reversetxt.text = reverseFormatter.format(validatetxt.text);
if(reverseFormatter.error != null){
Alert.show(reverseFormatter.error, "Formatter Error");
}
}
]]>
</mx:Script>
<comps:ReverseFormatter id="reverseFormatter" />
<mx:Panel title="Reverse Formatter" width="100%" height="100%" layout="absolute">
<mx:TextInput id="validatetxt" width="400" change="this.format()" y="30" x="3"/>
<mx:TextInput id="reversetxt" width="400" x="3" y="60" editable="false"/>
<mx:Label width="400" x="3" y="5" text="Type into the field below and see the results"/>
</mx:Panel>
</mx:Application>
自定义验证组件需要扩展mx.validators.Validator类,并重写doValidation方法
package
{
/*
Authored by Rich Tretola
*/
import mx.validators.Validator;
import mx.validators.ValidationResult;
public class PasswordValidator extends Validator {
private var results:Array;
public function PasswordValidator() {
super();
}
// Override the doValidation() method.
override protected function doValidation(value:Object):Array {
results = [];
// Call super's doValidation().
results = super.doValidation(value);
// Return if super's doValidation contains errors (required would be an example).
if (results.length > 0){
return results;
}
// Check for min length
var dataString:String = String(value);
if (dataString.length < 6){
results.push(new ValidationResult(true, null, "Short", "Password must be at least 6 characters."));
return results;
}
// Check for max length (this can be set in the text component's maxChars property).
if (dataString.length > 10){
results.push(new ValidationResult(true, null, "Long", "Password must be no larger than 10 characters."));
return results;
}
// Check for at least 1 upper case letter.
if (dataString.search("[A-Z]")<0) {
results.push(new ValidationResult(true, null, "Upper", "Passwords must contain at least one upper case letter."));
return results;
}
// Check for at least 1 lower case letter.
if (dataString.search("[a-z]")<0) {
results.push(new ValidationResult(true, null, "Lower", "Passwords must contain at lease one lower case letter."));
return results;
}
// Check for at least 1 number.
if (dataString.search("[0-9]")<0) {
results.push(new ValidationResult(true, null, "Number", "Passwords must contain at least one number."));
return results;
}
return results;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
backgroundColor="#FFFFFF" width="550" height="300" xmlns:comps="*">
<comps:PasswordValidator required="true" source="{passwordtxt}" property="text"
trigger="{validatebtn}" triggerEvent="click" listener="{passwordtxt}"/>
<mx:Panel title="Password Validator" width="350" height="150">
<mx:TextArea width="100%" height="45" editable="false" borderStyle="none"
text="Password must be between 6-10 characters and contain at least 1 upper case, 1 lower case, and 1 number." />
<mx:HBox paddingLeft="3">
<mx:TextInput id="passwordtxt" width="75"/>
<mx:Button id="validatebtn" label="Validate Password"/>
</mx:HBox>
</mx:Panel>
</mx:Application>
posted on 2011-03-24 13:41
长春语林科技 阅读(291)
评论(0) 编辑 收藏 所属分类:
flex