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

时间:2023-03-09 08:31:05
Flex 内置验证器—验证用户输入

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

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

 <?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="867" height="530" minWidth="955" minHeight="600"> <fx:Script>
<![CDATA[
import mx.controls.Alert;
protected function click(event:MouseEvent):void
{
Alert.show("你的姓名:"+test1.text+"\n你的年龄:"+test2.text+"\n你的生日:"+test3.text);
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
<mx:StringValidator source="{test1}" property="text" minLength="3" maxLength="20"
trigger="{test_btn}" triggerEvent="click"
tooShortError="字符数不能少于3!" tooLongError="字符数不能多于20!" />
<s:NumberValidator source="{test2}" property="text" allowNegative="false"
negativeError="不允许为负数" trigger="{test_btn}" triggerEvent="click"
domain="int"/>
<mx:DateValidator source="{test3}" property="text" inputFormat="yyyy/mm/dd"
allowedFormatChars="/" trigger="{test_btn}" triggerEvent="click"/>
</fx:Declarations>
<s:VGroup width="381" height="215" horizontalCenter="0" verticalCenter="0">
<s:Label width="289" height="25" fontSize="20" text="Enter your name:"/>
<s:TextInput id="test1" />
<s:Label width="289" height="25" fontSize="20" text="Enter your age:"/>
<s:TextInput id="test2"/>
<s:Label width="289" height="25" fontSize="20" text="Enter your birth date:"/>
<s:TextInput id="test3"/>
<s:Button id="test_btn" width="109" height="25" label="提交" click="click(event)"/>
</s:VGroup>
</s:Application>

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