我们如何在flex中使用Compare验证器

时间:2012-08-09 09:15:16

标签: actionscript-3 flex flex4

Flex中是否有比较验证器?我搜索但无法找到它。有比较验证器的验证器吗?

我的示例是如何使用flex验证器检查密码和确认密码是否相同

<s:TextInput id="password" width="218" textAlign="left" contentBackgroundColor="#FFFFFF"/>
<s:TextInput id="confirmpassword" width="218" textAlign="left" contentBackgroundColor="#FFFFFF"/> 

1 个答案:

答案 0 :(得分:2)

拥有以下2个电子邮件字段,首先我们定义一个标准的电子邮件验证器,并且为了确认我们在答案的最后定义了一个自定义的电子邮件验证器。

<mx:HBox
    verticalAlign   = "middle"
    color           = "#101010">
    <mx:Label
        text        = "E-Mail Address:"
        width       = "120"/>
    <mx:TextInput
        id          = "txtEmail"
        width       = "220"/>
</mx:HBox>
<mx:HBox
    verticalAlign   = "middle"
    color           = "#101010">
    <mx:Text
        text        = "Rewrite E-Mail Address:"
        width       = "120"/>
    <mx:TextInput
        id          = "txtEmail2"
        width       = "220"/>
</mx:HBox> 

现在我们添加验证器

<mx:EmailValidator
    id              = "valEmail"
    required        = "true"
    source          = "{txtEmail}"
    property        = "text"
    invalidCharError            = "Invalid format"
    invalidDomainError          = "Invalid format"
    invalidIPDomainError        = "Invalid format"
    invalidPeriodsInDomainError = "Invalid format"
    missingAtSignError          = "Invalid format"
    missingPeriodInDomainError  = "Invalid format"
    missingUsernameError        = "Invalid format"
    />

<validators:EmailConfirmationValidator
    id              = "valEmail2"
    required        = "true"
    source          = "{txtEmail2}"
    property        = "text"
    confirmationSource  = "{txtEmail}"
    confirmationProperty= "text"/>

另外我们需要定义我在“验证器”包中声明的EmailConfirmationValidator类

package validators
{
import mx.validators.ValidationResult;
import mx.validators.Validator;

public class EmailConfirmationValidator extends Validator
{
    public var confirmationSource: Object;
    public var confirmationProperty: String;

    public function EmailConfirmationValidator()
    {
        super();
    }

    // Define the doValidation() method.
    override protected function doValidation(value:Object):Array 
    {
        // Call base class doValidation().
        var results:Array = super.doValidation(value);

        if (value.text != value.confirmation)
        {
            results.push(new ValidationResult(true, null, "Mismatch",
                "Emails do not match!"));
        }

        return results;
    }       

    /**
     *  @private
     *  Grabs the data for the confirmation password from its different sources
     *  if its there and bundles it to be processed by the doValidation routine.
     */
    override protected function getValueFromSource():Object
    {
        var value:Object = {};

        value.text = super.getValueFromSource();

        if (confirmationSource && confirmationProperty)
        {
            value.confirmation = confirmationSource[confirmationProperty];
        }

        return  value;
    }  


}
}

如果您需要手动检查表单中的验证器。例如,在提交注册内容之前,请使用以下方法

public function isValid():Boolean
{
var validators:Array            = [valEmail, valEmail2];
    var validatorErrorArray:Array   = Validator.validateAll(validators);;
var isValidForm:Boolean         = validatorErrorArray.length == 0;

    return isValidForm;
}