1 validate
<?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" minWidth="955" minHeight="600"> <fx:Declarations> <mx:Validator source="{username}" property="text" required="true" /> </fx:Declarations> <s:VGroup horizontalCenter="0" verticalCenter="0"> <s:Label text="输入你的名字"/> <s:TextInput id="username"/> </s:VGroup> </s:Application>
2StringValidate
<?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" minWidth="955" minHeight="600"> <fx:Declarations> <mx:StringValidator source="{username}" property="text" minLength="3" maxLength="20" trigger="{submitButton}" triggerEvent="click" tooShortError="最少要有3个字符" tooLongError="最多只能20个字符" > </mx:StringValidator> </fx:Declarations> <s:VGroup horizontalCenter="0" verticalCenter="0"> <s:Label text="输入你的名字"/> <s:TextInput id="username"/> <s:Button label="Submit" id="submitButton"/> </s:VGroup> </s:Application>
3 NumberValidator
<?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" minWidth="955" minHeight="600"> <fx:Declarations> <mx:NumberValidator source="{age}" property="text" allowNegative="false" negativeError="年龄不太对昂" minValue="5" maxValue="110" domain="int" trigger="{submitButton}" triggerEvent="click" /> </fx:Declarations> <s:VGroup horizontalCenter="0" verticalCenter="0"> <s:Label text="输入你的年龄"/> <s:TextInput id="age" /> <s:Button label="Submit" id="submitButton"/> </s:VGroup> </s:Application>
但是 negativeError="报错内容" 似乎不太起作用
4 DateVlidator
<?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" minWidth="955" minHeight="600"> <fx:Declarations> <mx:DateValidator source="{birthday}" property="text" inputFormat="mm/dd/yyyy" allowedFormatChars="/" trigger="{submitButton}" triggerEvent="click" /> </fx:Declarations> <s:VGroup horizontalCenter="0" verticalCenter="0"> <s:Label text="请输入日期"/> <s:TextInput id="birthday"/> <s:Button label="Submit" id="submitButton"/> </s:VGroup> </s:Application>
5 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" minWidth="955" minHeight="600"> <fx:Declarations> <mx:DateValidator monthSource="{month}" monthProperty="value" daySource="{day}" dayProperty="value" yearSource="{year}" yearProperty="text" property="text" inputFormat="mm/dd/yyyy" allowedFormatChars="/" trigger="{submitButton}" triggerEvent="click" /> </fx:Declarations> <s:VGroup horizontalCenter="0" verticalCenter="0"> <s:Label text="请输入日期月"/> <s:NumericStepper id="month"/> <s:Label text="请输入日期日"/> <s:NumericStepper id="day"/> <s:Label text="请输入日期年"/> <s:TextInput id="year" width="60"/> <s:Button label="Submit" id="submitButton" /> </s:VGroup> </s:Application>
6 EmailValidator
<?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" minWidth="955" minHeight="600"> <fx:Declarations> <mx:EmailValidator source="{email}" property="text" invalidCharError="你输入的邮箱格式不正确" trigger="{submitButton}" triggerEvent="click" /> </fx:Declarations> <s:VGroup horizontalCenter="0" verticalCenter="0"> <s:Label text="Email:"/> <s:TextInput id="email"/> <s:Button label="提交" id="submitButton"/> </s:VGroup> </s:Application>
7 CreditCardValidator
<?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" minWidth="955" minHeight="600"> <fx:Declarations> <mx:CreditCardValidator cardNumberSource="{cardNumber}" cardNumberProperty="text" cardTypeSource="{cardType}" cardTypeProperty="selectedItem" trigger="{submitButton}" triggerEvent="click" /> </fx:Declarations> <s:VGroup horizontalCenter="0" verticalCenter="0"> <s:DropDownList id="cardType" width="150"> <s:ArrayCollection> <fx:String>American Express</fx:String> <fx:String>Visa</fx:String> <fx:String>Diners</fx:String> <fx:String>Discover</fx:String> <fx:String>MasterCard</fx:String> </s:ArrayCollection> </s:DropDownList> <s:Label text="Card Number"/> <s:TextInput id="cardNumber"/> <s:Button label="Submit" id="submitButton"/> </s:VGroup> </s:Application>
8 PhoneNumberValidator
8 RegExpValidator 正则表达式
ssn(Social Security Number)以美国社保账号为例
9用正则表达式 RegExpValidator 查找与模式匹配的所有匹配项
<?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" minWidth="955" minHeight="600"> <fx:Declarations> <mx:RegExpValidator source="{ssn}" property="text" flags="gmi" expression="d{3}.d{2}.d{4}" noMatchError="你的社保账号输入的不正确" trigger="{submitButton}" triggerEvent="click" > </mx:RegExpValidator> </fx:Declarations> <s:VGroup horizontalCenter="0" verticalCenter="0"> <s:Label text="美国社保号"/> <s:TextInput id="ssn"/> <s:Button label="提交" id="submitButton"/> </s:VGroup> </s:Application>
flags="gmi" 是忽略大小写
10查找与模式匹配的所有匹配项
<?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" minWidth="955" minHeight="600"> <fx:Script> <![CDATA[ import mx.controls.Alert; import mx.events.ValidationResultEvent; import mx.validators.RegExpValidationResult; private function handleValidation(event:ValidationResultEvent):void { var oneResult:RegExpValidationResult; for(var i:int =0; i<event.results.length;i++) { oneResult = event.results[i]; Alert.show("找到一个匹配zaiindex中: "+ oneResult.matchedIndex +" 在characters of"+oneResult.matchedString,"RegEx Results",Alert.NONMODAL); } } ]]> </fx:Script> <fx:Declarations> <mx:RegExpValidator source="{test}" property="text" flags="gmi" valid="handleValidation(event)" expression="m[ai]n" noMatchError="我不喜欢这个" trigger="{submitButton}" triggerEvent="click"/> </fx:Declarations> <s:VGroup horizontalCenter="0" verticalCenter="0"> <s:Label text="Try me:"/> <s:TextInput id="test"/> <s:Button label="Submit" id="submitButton" /> </s:VGroup> </s:Application>
11 实时验证
<?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" minWidth="955" minHeight="600"> <fx:Declarations> <mx:StringValidator source="{address}" minLength="5" property="text" trigger="{address}" triggerEvent="change"/> </fx:Declarations> <s:VGroup horizontalCenter="0" verticalCenter="0"> <s:Label text="请输入你的地址"/> <s:TextInput id="address"/> <s:Button label="提交" id="submitButton" /> </s:VGroup> </s:Application>
12 提交值验证,提交值包括Tab键、回车键、方向键或鼠标单击其他组件
<?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" minWidth="955" minHeight="600"> <fx:Declarations> <mx:StringValidator source="{address}" minLength="5" property="text" trigger="{address}" triggerEvent="valueCommit"/> </fx:Declarations> <s:VGroup horizontalCenter="0" verticalCenter="0"> <s:Label text="请输入你的地址"/> <s:TextInput id="address"/> <s:Button label="提交" id="submitButton" /> </s:VGroup> </s:Application>
13 通过性验证
<?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" minWidth="955" minHeight="600"> <fx:Declarations> <mx:StringValidator source="{username}" minLength="6" property="text" trigger="{submitButton}" triggerEvent="click"/> <mx:EmailValidator source="{email}" property="text" invalidCharError="你输入的邮箱格式不正确" trigger="{submitButton}" triggerEvent="click" /> </fx:Declarations> <s:VGroup horizontalCenter="0" verticalCenter="0"> <s:Label text="输入EMAIL"/> <s:TextInput id="email"/> <s:Label text="输入你的名字"/> <s:TextInput id="username"/> <s:Button label="Submit" id="submitButton"/> </s:VGroup> </s:Application>