flex中validateall()方法实现多Item验证且结果统一提示
程序员文章站
2023-12-30 08:11:40
复制代码 代码如下:
<?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>
复制代码 代码如下:
<?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>