views:

195

answers:

1

I have seen several examples in Flex for passowrd validator using RegExp. But every where the validation is happend for single validation.

I have a requirement, like password validations like

• At least one Upper case letter

• At least one numeric character

• At least one special character such as @, #, $, etc.

• At least one Lower case letter

• password lenght minimum 6 digits

• password cannot be same as user name

Can anyone provide me a code for this..? I have the code only for checking the password is valid or not . check the below code.

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 Code:

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  A: 

See below the 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>

The validator class:

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;

    }
}
Cornel Creanga
Thanks Cornel, it was working fine.
Ravi K Chowdary