Flex笔记_验证用户输入
发布时间:2020-12-15 03:39:23 所属栏目:百科 来源:网络整理
导读:内置验证器 Flex提供了很多内置的验证器,它们都是Validator类的子类。 Flex以扩展Validator类的方式创建了内置验证器,以用于满足常用需求。 Validator组件的重要属性和函数: enabled:Boolean类型,决定是否启用验证功能 required:Boolean类型,是否必填
内置验证器
Validator
<?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="Enter your name:"/> <s:TextInput id="username"/> <s:TextInput/> </s:VGroup> </s:Application> StringVAlidator
<?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 property="text" source="{username}" minLength="3" maxLength="20" trigger="{submitButton}" triggerEvent="click" tooLongError="too long,longer than 20" tooShortError="too short,shoter than 3"/> </fx:Declarations> <s:VGroup horizontalCenter="0" verticalCenter="0"> <s:Label text="Enter your name:"/> <s:TextInput id="username"/> <s:Button label="Submit" id="submitButton"/> </s:VGroup> </s:Application> 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="not allow negative" maxValue="110" minValue="5" domain="int" trigger="{submitButton}" triggerEvent="click"/> </fx:Declarations> <s:VGroup horizontalCenter="0" verticalCenter="0"> <s:Label text="Enter your age:"/> <s:TextInput id="age"/> <s:Button label="Submit" id="submitButton"/> </s:VGroup> </s:Application> 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" trigger="{submitButton}" triggerEvent="click"/> </fx:Declarations> <s:HGroup horizontalCenter="0" verticalCenter="0"> <s:Label text="Month:"/> <s:NumericStepper id="month"/> <s:Label text="Day:"/> <s:NumericStepper id="day"/> <s:Label text="Year:"/> <s:TextInput id="year" width="60"/> <s:Button label="Submit" id="submitButton"/> </s:HGroup> </s:Application> 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="you've got some funky characters" trigger="{submitButton}" triggerEvent="click"/> </fx:Declarations> <s:VGroup horizontalCenter="0" verticalCenter="0"> <s:Label text="Email:"/> <s:TextInput id="email"/> <s:Button label="Submit" id="submitButton"/> </s:VGroup> </s:Application> 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"> <mx:ArrayCollection> <fx:String>American Express</fx:String> <fx:String>Visa</fx:String> <fx:String>Diners Club</fx:String> <fx:String>Discover</fx:String> <fx:String>MasterCard</fx:String> </mx:ArrayCollection> </s:DropDownList> <s:Label text="Card Number:"/> <s:TextInput id="cardNumber"/> <s:Button label="Submit" id="submitButton"/> </s:VGroup> </s:Application> CurrencyValidator
<?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:CurrencyValidator source="{income}" property="text" allowNegative="false" negativeError="dont support negative income..." precision="2" precisionError="support 2 decimals only." trigger="{submitButton}" triggerEvent="click"/> </fx:Declarations> <s:VGroup horizontalCenter="0" verticalCenter="0"> <s:Label text="How much do you make?"/> <s:TextInput id="income"/> <s:Button label="Submit" id="submitButton"/> </s:VGroup> </s:Application> PhoneNumberValidator
<?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:PhoneNumberValidator source="{phone}" property="text" trigger="{submitButton}" triggerEvent="click"/> </fx:Declarations> <s:VGroup horizontalCenter="0" verticalCenter="0"> <s:Label text="What number can we reach you at?"/> <s:TextInput id="phone"/> <s:Button label="Submit" id="submitButton"/> </s:VGroup> </s:Application> 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="Your SSN is unrecognized." trigger="{submitButton}" triggerEvent="click"/> </fx:Declarations> <s:VGroup horizontalCenter="0" verticalCenter="0"> <s:Label text="Social Security Number:"/> <s:TextInput id="ssn"/> <s:Button label="Submit" id="submitButton"/> </s:VGroup> </s:Application> <?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.events.ValidationResultEvent; import mx.validators.RegExpValidationResult; import mx.controls.Alert; 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("Found a match at Index:" + oneResult.matchedIndex + "n On character 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="I don't like it!" 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> SocialSecurityValidatorZipCodeValidator实时验证
<?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="What your address:"/> <s:TextInput id="address"/> <s:Button label="Submit" id="submitButton"/> </s:VGroup> </s:Application> 提交值验证
<?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="What your address:"/> <s:TextInput id="address"/> <s:Button label="Submit" id="submitButton"/> </s:VGroup> </s:Application> 通过性验证
脚本式验证
<?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.EmailValidator; import mx.validators.StringValidator; public var emailVal:EmailValidator = new EmailValidator(); public var stringVal:StringValidator = new StringValidator(); public function validateForm():void { var valResult:ValidationResultEvent; stringVal.source = username; stringVal.property = "text"; stringVal.minLength = 6; emailVal.source = email; emailVal.property = "text"; valResult = emailVal.validate(); if(valResult.type == "invalid") { Alert.show("Please fix your email address:"); } else { valResult = stringVal.validate(); if(valResult.type == "invalid") { Alert.show("Please fix your Username."); } } } ]]> </fx:Script> <s:VGroup horizontalCenter="0" verticalCenter="0"> <s:Label text="Email :"/> <s:TextInput id="email"/> <s:Label text="Your name:"/> <s:TextInput id="username"/> <s:Button label="Submit" id="submitButton" click="validateForm()"/> </s:VGroup> </s:Application> 验证技巧
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- React-Native -- 可继承的Navigator
- ruby – 如何使用具有特殊字符的用户名或密码解析FTP URL?
- flex 文件上传与下载
- 文件上传 – Rails3,Cucumber,Capybara,文件上传=>内容正文
- c# – 用ref或out参数编写iron python方法
- react-native运行时出现java.lang.RuntimeException: SDK l
- Oracle 11g SQL在多列查询的一列中获取唯一值
- 依赖注入性能测试(spring.net 和Enterprise Library)
- swift中UIWebView的使用
- 关于duilib中的list的扩展探索