<?xml version="1.0" encoding="utf-8"?>
<!-- Simple example to demonstrate the NumberValidator. -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
import mx.controls.Alert;
</mx:Script>
<mx:NumberValidator source="{age}" property="text" integerError="Enter Integer value"
minValue="18" maxValue="50" domain="int"
trigger="{myButton}" triggerEvent="click"
valid="Alert.show('Validation Succeeded!');"/>
<mx:Panel title="NumberValidator Example" width="75%" height="75%"
paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom="10">
<mx:Form>
<mx:FormItem label="Enter an age between 18 and 50: ">
<mx:TextInput id="age" width="100%"/>
</mx:FormItem>
<mx:FormItem >
<mx:Button id="myButton" label="Validate" />
</mx:FormItem>
</mx:Form>
</mx:Panel>
</mx:Application>
===============================================================
<mx:NumberValidator minValue="1" maxValue="{int.MAX_VALUE}" lowerThanMinError="lower than 1"
source="{txt}" property="text" integerError="Enter Integer value"
exceedsMaxError="no more than int.MAX_VALUE 2,147,483,647"
trigger="{btn3}" triggerEvent="click" valid="validate()"/>
public function validate():void{
Alert.show("it is ok");
}
=============================
通过设置NumberValidator的domain(范围)属性,我们可以设定数字的范围是整数还是实数(浮点数)。
请看如下代码:
- <?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>