在Flex中使用RegEx进行密码验证

时间:2010-05-20 11:41:15

标签: flex flexbuilder

我在Flex中看到了几个使用RegExp的passowrd验证器的例子。但是每次验证都会进行单一验证。

我有一个要求,比如

等密码验证

•至少一个大写字母

•至少一个数字字符

•至少有一个特殊字符,如@,#,$等

•至少一个小写字母

•密码长度至少6位

•密码不能与用户名相同

任何人都可以为我提供代码..?我有代码只用于检查密码是否有效。检查以下代码。

MXML CODE

<mx:FormItem label="Username:" x="83" y="96" width="66">
    </mx:FormItem>
    <mx:FormItem label="Password:"  x="88" y="123"  width="61">
    </mx:FormItem>
    <mx:Button label="Login" id="btnLogin" tabIndex="2" click="login();" enabled="{formIsValid}"  x="327" y="162" width="84"/>
    <mx:TextInput id="txtPassword" displayAsPassword="true" change="validateForm(event);"   x="152" y="121" width="217"/>
    <mx:TextInput id="txtUserId" change="validateForm(event);"  x="152" y="94" width="217"/>

AS代码:

private function validateForm(event:Event):void 
{                    

    focussedFormControl = event.target as DisplayObject;         
    formIsValid = true;            
    formIsEmpty = (txtUserId.text == "" && txtPassword.text == ""); 

    validate(strVUserId);                
    validate(strVPassword); 
}

 private function validate(validator:Validator):Boolean
 {                

    var validatorSource:DisplayObject = validator.source as DisplayObject;
    var suppressEvents:Boolean = (validatorSource != focussedFormControl);
    var event:ValidationResultEvent = validator.validate(null, suppressEvents); 
    var currentControlIsValid:Boolean = (event.type == ValidationResultEvent.VALID);

    formIsValid = formIsValid && currentControlIsValid;

    return currentControlIsValid;
  }

1 个答案:

答案 0 :(得分:2)

见下面的mxml。

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="*">

<mx:Model id="model">
    <name>
        <data>
            <userName>
                {username.text}
            </userName>
            <password>
                {password.text}
            </password>         
        </data>
    </name>
</mx:Model>

<local:PasswordValidator id="passwordValidator" source="{model}" property="data" listener="{password}" minLength="6"/>
<mx:Form>

    <mx:FormItem label="Username:" >
        <mx:TextInput id="username"/>
    </mx:FormItem>
    <mx:FormItem label="Password:" >
        <mx:TextInput id="password" displayAsPassword="true" />
    </mx:FormItem>
    <mx:FormItem >
        <mx:Button label="Ok" click="passwordValidator.validate()"/>
    </mx:FormItem>

</mx:Form>

验证员类:

public class PasswordValidator extends Validator
{
    public function PasswordValidator()
    {
    }

    private var errors:Array;

    private var _minLength:int = 8;

    public function set minLength(value:int):void{
        _minLength = value;     
    }

    override protected function doValidation(value:Object):Array {

        var userName: String = value.userName;
        var password: String = value.password;

        var pattern:RegExp = new RegExp("^.*(?=.{"+_minLength+",})(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$");             

        errors = super.doValidation(value);     

        if(userName == password){
            errors.push(new ValidationResult(true, null,null,"Password should not be identical with the username"));
        }

        if (pattern.exec(password)==null){
            errors.push(new ValidationResult(true, null,null,"Weak password"));
        }   

        return errors;

    }
}