Flex 各种校验
Flex 各种校验:
参考:http://blog.csdn.net/jerry_bj/article/details/5650469
参考正则表达式:http://www.cnblogs.com/feilong3540717/archive/2012/11/21/2780141.html
<?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" height="517"> <fx:Script> <![CDATA[ import mx.validators.Validator; import mx.validators.ZipCodeValidatorDomainType; import spark.events.TextOperationEvent; protected function txt007_changeHandler(event:TextOperationEvent):void { if(txt007.text == 'a'){ txt007.errorString = 'a'; }else { txt007.errorString = 'others'; } } protected function btn003_clickHandler(event:MouseEvent):void { vld010.validate(); } protected function btn002_clickHandler(event:MouseEvent):void { var validArray:Array; validArray = [vld001,vld002]; Validator.validateAll(validArray); } ]]> </fx:Script> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> <mx:EmailValidator id="vld001" property="text" source="{txt001}"/> <mx:PhoneNumberValidator id="vld002" property="text" required="false" source="{txt002}"/> <mx:ZipCodeValidator id="vld003" domain="{ZipCodeValidatorDomainType.US_OR_CANADA}" property="text" required="false" source="{txt003}"/> <s:NumberValidator id="vld004" property="text" source="{txt004}"/> <mx:RegExpValidator id="vld005" expression="{'^[1-9]$'}" flags="g" property="text" source="{txt005}"/> <mx:StringValidator id="vld006" property="text" requiredFieldError="ABC..." source="{txt006}"/> <mx:EmailValidator id="vld008" source="{txt008}" property="text" triggerEvent="change" /> <mx:EmailValidator id="vld009" source="{txt009}" property="text" trigger="{btn001}" triggerEvent="click" /> <mx:EmailValidator id="vld010" source="{txt010}" property="text" /> </fx:Declarations> <!-- <![CDATA[ 使用flex自身的RegExpValidator控件进行正则表达式校验十分方便,但是需要注意以下几点: 1.必须用{''}将正则表达式括起来。 2.所有的转义符号\都必须写成\\。 3.大括号{}必须加转义符号。 例: <mx:RegExpValidator id="regExpV" source="{str}" property="text" flags="g" expression="{'^[1-9](\\d*\\.\\d\{1,2\}|\\d*)$'}" noMatchError="版本形式不合法"/> ]]> --> <s:Form x="45" y="43"> <s:FormHeading label="各种验证"/> <s:FormItem label="email"> <s:TextInput id="txt001"/> </s:FormItem> <s:FormItem label="telnum"> <s:TextInput id="txt002"/> </s:FormItem> <s:FormItem label="zipcode"> <s:TextInput id="txt003"/> </s:FormItem> <s:FormItem label="number"> <s:TextInput id="txt004"/> </s:FormItem> <s:FormItem label="regEx"> <s:TextInput id="txt005"/> </s:FormItem> <s:FormItem label="string"> <s:TextInput id="txt006"/> </s:FormItem> <s:Button id="btn002" label="统一进行验证" click="btn002_clickHandler(event)"/> </s:Form> <s:Form x="300" y="43"> <s:FormHeading label="errorString"/> <s:FormItem label="errorS"> <s:TextInput id="txt007" change="txt007_changeHandler(event)"/> </s:FormItem> </s:Form> <s:Form x="300" y="155"> <s:FormHeading label="triggerEvent"/> <s:FormItem label="email01"> <s:TextInput id="txt008"/> </s:FormItem> <s:FormItem label="email02"> <s:TextInput id="txt009"/> <s:Button label="trigger" id="btn001"/> </s:FormItem> <s:FormItem label="email03"> <s:TextInput id="txt010"/> <s:Button id="btn003" label="valid" click="btn003_clickHandler(event)"/> </s:FormItem> </s:Form> </s:Application>