Flex 内置验证器—验证用户输入

今晚对于Flex中的Validator类(所有验证器的父类)测试一下

----》其中常用的验证类有StringValidator,NumberValidator,DateValidator 测试如下:

 1 <?xml version="1.0" encoding="utf-8"?>

 2 <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

 3                xmlns:s="library://ns.adobe.com/flex/spark"

 4                xmlns:mx="library://ns.adobe.com/flex/mx"

 5                width="867" height="530" minWidth="955" minHeight="600">

 6     

 7     <fx:Script>

 8         <![CDATA[

 9             import mx.controls.Alert;        

10             protected function click(event:MouseEvent):void

11             {

12                 Alert.show("你的姓名:"+test1.text+"\n你的年龄:"+test2.text+"\n你的生日:"+test3.text);

13             }            

14         ]]>

15     </fx:Script>    

16     <fx:Declarations>

17         <!-- 将非可视元素(例如服务、值对象)放在此处 -->

18         <mx:StringValidator source="{test1}" property="text" minLength="3" maxLength="20"

19                             trigger="{test_btn}" triggerEvent="click"

20                             tooShortError="字符数不能少于3!" tooLongError="字符数不能多于20!" />

21         <s:NumberValidator source="{test2}" property="text" allowNegative="false"

22                            negativeError="不允许为负数" trigger="{test_btn}" triggerEvent="click"

23                            domain="int"/>

24         <mx:DateValidator source="{test3}" property="text" inputFormat="yyyy/mm/dd"

25                           allowedFormatChars="/" trigger="{test_btn}" triggerEvent="click"/>

26     </fx:Declarations>

27     <s:VGroup width="381" height="215" horizontalCenter="0" verticalCenter="0">

28       <s:Label  width="289" height="25" fontSize="20" text="Enter your name:"/>

29       <s:TextInput id="test1"  />

30       <s:Label width="289" height="25" fontSize="20" text="Enter your age:"/>

31       <s:TextInput id="test2"/>

32       <s:Label width="289" height="25" fontSize="20" text="Enter your birth date:"/>

33       <s:TextInput id="test3"/>

34       <s:Button id="test_btn" width="109" height="25" label="提交" click="click(event)"/>

35     </s:VGroup>

36 </s:Application>

此外还有EmailValida、CreditValidator、CurrencyValidator、PhoneNumberValidator等都是常用的内置验证器~

你可能感兴趣的:(Flex)