Tuesday, May 13, 2008

Password validator in flex 3


create a validator PwdValidator.as

package valueObjects

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

public class PwdValidator extends Validator {

// Define Array for the return value of doValidation().
private var results:Array;
// this will help in getting the status
public static var VALID:Boolean = true;


// Constructor.
public function PwdValidator() {
// Call base class constructor.
super();


}

// Define the doValidation() method.
override protected function doValidation(value:Object):Array {
//var pwd: Password = value as Password;
var p1: String = value.first;
var p2: String = value.second;

results = [];
results = super.doValidation(value);


if(p1 == p2)
{
VALID=true;
return results;
}
else
{
results.push(new ValidationResult(true, null, "Mismatch",
"Password Dosen't match Retype!"));
VALID=false;
return results;
}
}
}

}


Now the flex code should be passing the value of the password and confirm password textbox. This is done as

<mx:Model id ="pass">
<name>
<passwords>

<first>{txtPassword.text}</first>
<second>{txtConfirmPassword.text}</second>

</passwords>
</name>
</mx:Model>

// this is the way to pass the multiple controls input to the validator.
<val:PwdValidator id="custPass" source="{pass}" property="passwords" listener="{txtPassword}"/>

<mx:Form labelWidth="110">

<mx:FormItem required="true" label="Password">
<mx:TextInput id="txtPassword" displayAsPassword="true" />
</mx:FormItem>
<mx:FormItem required="true" label="Confirm Password">
<mx:TextInput id="txtConfirmPassword" displayAsPassword="true" change="custPass.validate()"/>
</mx:FormItem>

</mx:Form>