flex 用户名验证功能:NumberValidator类验证整数
<?xml version="1.0" encoding="utf-8"?><!-- http://blog.flexexamples.com/2008/08/30/validating-integers-using-the-numbervalidator-class/ --><mx:Application name="NumberValidator_domain_test" xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white"> <mx:Script> <![CDATA[ import mx.controls.Alert; import mx.events.ValidationResultEvent; //验证失败后的响应 private function numberValidator_invalid(evt:ValidationResultEvent):void { Alert.show(evt.message); } //验证成功后的响应 private function numberValidator_valid(evt:ValidationResultEvent):void { Alert.show(evt.type); } ]]> </mx:Script> <mx:NumberValidator id="numberValidator" domain="{comboBox.selectedItem}" source="{textInput}" property="text" trigger="{button}" triggerEvent="click" invalid="numberValidator_invalid(event);" valid="numberValidator_valid(event);" /> <mx:ApplicationControlBar dock="true"> <mx:Form styleName="plain"> <mx:FormItem label="domain:"> <mx:ComboBox id="comboBox" dataProvider="[real,int]" /> </mx:FormItem> </mx:Form> </mx:ApplicationControlBar> <mx:Form defaultButton="{button}"> <mx:FormItem direction="horizontal"> <mx:TextInput id="textInput" restrict="0-9.-" maxChars="10" /> <mx:Button id="button" label="validate" /> </mx:FormItem> </mx:Form></mx:Application>
<?xml version="1.0" encoding="utf-8"?><!-- http://blog.flexexamples.com/2008/08/30/validating-integers-using-the-numbervalidator-class/ --><mx:Application name="NumberValidator_domain_test" xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white"> <mx:Script> <![CDATA[ import mx.controls.Alert; import mx.events.ValidationResultEvent; //验证失败后的响应 private function numberValidator_invalid(evt:ValidationResultEvent):void { Alert.show(evt.message); } //验证成功后的响应 private function numberValidator_valid(evt:ValidationResultEvent):void { Alert.show(evt.type); } ]]> </mx:Script> <mx:NumberValidator id="numberValidator" domain="{comboBox.selectedItem}" //domain属性,确定实数还是整数范围的关键 source="{textInput}" //源 property="text" trigger="{button}" //由谁引发验证 triggerEvent="click" //由什么事件引发验证 invalid="numberValidator_invalid(event);" //验证失败后的侦听器 valid="numberValidator_valid(event);" /> //验证成功后的侦听器 <mx:ApplicationControlBar dock="true"> <mx:Form styleName="plain"> <mx:FormItem label="domain:"> <mx:ComboBox id="comboBox" dataProvider="[real,int]" /> </mx:FormItem> </mx:Form> </mx:ApplicationControlBar> <mx:Form defaultButton="{button}"> <mx:FormItem direction="horizontal"> <mx:TextInput id="textInput" restrict="0-9.-" //限制可输入文本框的内容 maxChars="10" /> //最大字符数 <mx:Button id="button" label="validate" /> </mx:FormItem> </mx:Form></mx:Application>