---
title: "自定义二次确认验证控件"
author: "free_zhou"
date: "2010-07-04T11:48:50Z"
tags: ["Flash", "Flex", "XML"]
categories: ["Web前端"]
slug: "untitled"
draft: false
lastmod: "2010-07-04T11:59:02Z"
id: 705299
url: "https://free-zhou.iteye.com/blog/705299"
---

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

```js

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文件

```java

<?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>

```