MXML 部分:
<myValidators:PwdValidator id="valPwd" source="{this.txtRePwd}" required="true" property="text" rePwd="{this.txtPwd.text}" requiredFieldError="必须填写密码!"/>
Actionscript 部分:
package myValidators
{
import mx.validators.Validator;
import mx.validators.ValidationResult;
public class PwdValidator extends Validator {
[Property("rePwd")]
private var m_rePwd:String;
public function set rePwd(value:String):void
{
m_rePwd=value;
}
public function get rePwd():String
{
return m_rePwd;
}
private var results:Array;
public function PwdValidator() {
super();
}
override protected function doValidation(value:Object):Array {
results = [];
results = super.doValidation(value);
if (results.length > 0)
return results;
if(value.toString().length<6||value.toString().length>20)
{
results.push(new ValidationResult(true, null, "tooShort",
"密码必须是6-20位"));
return results;
}
if(value.toString()!=rePwd)
{
results.push(new ValidationResult(true, null, "different",
"两次密码输入必须相同"));
return results;
}
return results;
}
}
}