NumberValidate

<?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(范围)属性,我们可以设定数字的范围是整数还是实数(浮点数)。

请看如下代码:

 

Java代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <!-- http://blog.flexexamples.com/2008/08/30/validating-integers-using-the-numbervalidator-class/ -->   
  3. <mx:Application name="NumberValidator_domain_test"  
  4.         xmlns:mx="http://www.adobe.com/2006/mxml"  
  5.         layout="vertical"  
  6.         verticalAlign="middle"  
  7.         backgroundColor="white">   
  8.   
  9.     <mx:Script>   
  10.         <![CDATA[   
  11.             import mx.controls.Alert;   
  12.             import mx.events.ValidationResultEvent;   
  13.             //验证失败后的响应   
  14.             private function numberValidator_invalid(evt:ValidationResultEvent):void {   
  15.                 Alert.show(evt.message);   
  16.             }   
  17.             //验证成功后的响应   
  18.             private function numberValidator_valid(evt:ValidationResultEvent):void {   
  19.                 Alert.show(evt.type);   
  20.             }   
  21.         ]]>   
  22.     </mx:Script>   
  23.   
  24.     <mx:NumberValidator id="numberValidator"  
  25.             domain="{comboBox.selectedItem}"    //domain属性,确定实数还是整数范围的关键   
  26.             source="{textInput}"   //源   
  27.             property="text"              
  28.             trigger="{button}"        //由谁引发验证   
  29.             triggerEvent="click"       //由什么事件引发验证   
  30.             invalid="numberValidator_invalid(event);"   //验证失败后的侦听器   
  31.             valid="numberValidator_valid(event);" />    //验证成功后的侦听器   
  32.   
  33.     <mx:ApplicationControlBar dock="true">   
  34.         <mx:Form styleName="plain">   
  35.             <mx:FormItem label="domain:">   
  36.                 <mx:ComboBox id="comboBox"  
  37.                         dataProvider="[real,int]" />   
  38.             </mx:FormItem>   
  39.         </mx:Form>   
  40.     </mx:ApplicationControlBar>   
  41.   
  42.     <mx:Form defaultButton="{button}">   
  43.         <mx:FormItem direction="horizontal">   
  44.             <mx:TextInput id="textInput"  
  45.                     restrict="0-9.-"    //限制可输入文本框的内容   
  46.                     maxChars="10" />   //最大字符数   
  47.             <mx:Button id="button"  
  48.                     label="validate" />   
  49.         </mx:FormItem>   
  50.     </mx:Form>   
  51.   
  52. </mx:Application>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值