程序员

flex中validateall()方法实现多Item验证且结果统一提示

作者:admin 2021-04-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/sp...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)
复制代码 代码如下:

<?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"
creationComplete="initHandler(event)">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.events.ValidationResultEvent;
import mx.validators.Validator;

private var error1:String;

private var error2:String;

private var error3:String;

protected function initHandler(event:FlexEvent):void
{

}


protected function btn_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
var validateArray:Array=Validator.validateAll(
[NAMEValidator,AGEValidator,PHONEValidator]);
if(validateArray.length==0)
{
Alert.show("成功啦!");
}else
{
Alert.show(error1+"\n"+error2+"\n"+error3);
}
}

protected function NAMEValidator_invalidHandler(event:ValidationResultEvent):void
{
error1 =event.message;
}

protected function AGEValidator_invalidHandler(event:ValidationResultEvent):void
{
error2 =event.message;
}

protected function PHONEValidator_invalidHandler(event:ValidationResultEvent):void
{
error3 =event.message;
}

]]>
</fx:Script>
<fx:Declarations>
<mx:StringValidator id="NAMEValidator"
source="{username}"
property="text"
minLength="6" maxLength="16"
tooLongError="不能多于16个字符" tooShortError="不能低于六位"
required="true"
invalid="NAMEValidator_invalidHandler(event)"
/>
<mx:NumberValidator id="AGEValidator" source="{age}"
domain="int" allowNegative="false" maxValue="60" minValue="18"
required="true" property="text"
exceedsMaxError="不能大于60" lowerThanMinError="不能低于十八"
invalid="AGEValidator_invalidHandler(event)"
/>
<mx:PhoneNumberValidator id="PHONEValidator"
minDigits="11" property="text"
source="{phone}"
required="true"
invalid="PHONEValidator_invalidHandler(event)"/>
</fx:Declarations>
<mx:Panel>
<mx:Form>
<mx:FormItem label="name">
<mx:TextInput id="username"/>
</mx:FormItem>
<mx:FormItem label="age">
<mx:TextInput id="age"/>
</mx:FormItem>
<mx:FormItem label="phone">
<mx:TextInput id="phone"/>
</mx:FormItem>
<mx:FormItem>
<mx:Button label="验证" id="btn" click="btn_clickHandler(event)"/>
</mx:FormItem>
</mx:Form>
</mx:Panel>
</s:Application>

本文转载自网络,原文链接:https://m.jb51.net/article/41736.htm

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • 四两拨千斤——你不知道的VScode编码Ty

    四两拨千斤——你不知道的VScode编码Ty

  • 我是如何在 Vue 项目中做代码分割的

    我是如何在 Vue 项目中做代码分割的

  • position:sticky 粘性定位的几种巧妙应

    position:sticky 粘性定位的几种巧妙应

  • 从零到一搭建React组件库

    从零到一搭建React组件库

腾讯云代理商
精彩导读
海外云服务器
热门资讯
腾讯云代理商