多场验证

use*_*084 1 apache-flex actionscript-3

我有一个简单的表格,包含电子邮件和密码.我想在电子邮件和密码有效后再显示提交按钮.我的想法是当我运行handleValidator函数时,我可以检查电子邮件的"有效"属性的某种值以及pass的"valid"属性.如何访问项目的有效属性?

<?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" 
            >
<fx:Script>
    import mx.events.ValidationResultEvent;
    private function handleValidator ( eventObj:ValidationResultEvent ):void {

        if( eventObj.type==ValidationResultEvent.VALID
                          // what goes here?????
                          //  && emailValidator.valid = true ?????
                          //  && passValidator.valid = true  ??????

                          ){ 
            submit.visible = true;
        }
        else {
            submit.visible = false;
        }
    }


</fx:Script>

<fx:Declarations>

    <mx:EmailValidator id="emailValidator" source="{email}" property="text"
                       trigger="{email}" triggerEvent="change"
                       valid="handleValidator(event)" invalid="handleValidator(event)"
                       />
    <mx:StringValidator id="passValidator" source="{pass}" property="text"
                        trigger="{pass}" triggerEvent="change"
                        valid="handleValidator(event)" invalid="handleValidator(event)"
                        minLength="5" maxLength="10"
                        />
</fx:Declarations>
<s:VGroup>

<s:TextInput id="email" />
<s:TextInput id="pass" />
<s:Button id="submit" visible="false" />

</s:VGroup>
</s:Application>
Run Code Online (Sandbox Code Playgroud)

2DH*_*2DH 6

尝试这样的事情.首先将验证器放入数组中:

<mx:Array id="validators">
    <mx:EmailValidator id="emailValidator" source="{email}" property="text">

    <mx:StringValidator id="passValidator" source="{pass}" property="text"
        minLength="5" maxLength="10"/>
</mx:Array>
Run Code Online (Sandbox Code Playgroud)

为您的输入添加更改事件处理程序:

<s:TextInput id="email" change="validate()"/>
<s:TextInput id="pass" change="validate()"/>
Run Code Online (Sandbox Code Playgroud)

处理程序将触发验证:

private function validate():void{
    //validate both your inputs simultaneously
    var errors:Array = Validator.validateAll(validators);
    if (errors.length>0){
        //hide submit button
    }else{
        //show submit button
    }
}
Run Code Online (Sandbox Code Playgroud)

就这样.