自定义二次确认验证控件

最近用flex,在验证的时候发现没有确认比较验证控件,在网上找一下发现泥水作坊的博客不错,借鉴一下高人的代码。记录下来,以防忘记
定义一个验证控件,继承了Validator
package confirm
{
	import mx.validators.ValidationResult;
	import mx.validators.Validator;

	public class ConfirmVlidator extends Validator
	{
		public function ConfirmVlidator()
		{
			super();
		}
		
		public var messages : Array;
		public var compareError : String;
		public var compareSource : Object;
		public var compareProperty : String;
		
		override protected function doValidation(value : Object) : Array
		{
			messages = [];
			messages = super.doValidation(value);
			if(messages.length > 0)
			{
				return messages;
			}
			if(source[property] != compareSource[compareProperty])
			{
				messages.push(new ValidationResult(true,null,null,compareError));
			}
			return messages;
		}
	}
}

引用mxml文件
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
	 xmlns:gshzhou="confirm.*">
	<mx:TextInput x="172" y="93" id="password"/>
	<mx:TextInput x="172" y="139" id="confirmPassword"/>
	<mx:Button x="218" y="224" id="testButton" label="test"/>
	
	<gshzhou:ConfirmVlidator source="{confirmPassword}" property="text" required="true" requiredFieldError="必须输入"
		 compareSource="{password}" compareProperty="text" compareError="2次输入的不一致"
		 trigger="{testButton}" triggerEvent="click">
		
	</gshzhou:ConfirmVlidator>
</mx:Application>


[flash=200,200]

[/flash]

你可能感兴趣的:(xml,Flex,Flash)