flex3 Validator

1.简单的验证demo
<?xml version="1.0"?>
<!-- Simple example to demonstrate the Validator class. -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.ValidationResultEvent;
private function handleValid(eventObj:ValidationResultEvent):void {
if(eventObj.type==ValidationResultEvent.VALID)    
    submitButton.enabled = true;
else
    submitButton.enabled = false;
}
private function submitForm():void {
Alert.show("Form Submitted!");
}
]]>
</mx:Script>
<mx:Validator id="reqValid" required="true"
source="{fname}" property="text" 
valid="handleValid(event)" invalid="handleValid(event)"/>       
<mx:Panel title="Validator Example" width="100%" height="100%" 
paddingTop="5" paddingLeft="5" paddingRight="5" paddingBottom="5">
<mx:Form>
<mx:Text width="100%" color="blue"
text="Enter a value in the Name field before you can submit. The E-mail field is optional."/>
<mx:FormItem label="Name: " required="true">
<mx:TextInput id="fname" width="100%"/>
</mx:FormItem>
<mx:FormItem label="E-mail address: " required="false">
<mx:TextInput id="email" width="100%"/>
</mx:FormItem>
<mx:FormItem>
<mx:Button id="submitButton" enabled="false" 
    label="Submit" click="submitForm();"/>
</mx:FormItem>
</mx:Form>
</mx:Panel>
</mx:Application>


2.triggerEvent 和 trigger 使用

<mx:ZipCodeValidator id="zipV" 
source="{myZip}" 
property="text" 
trigger="{mySubmit}" 
triggerEvent="click"/>
<mx:TextInput id="myZip"/> 
<mx:Label text="{myZip.errorString}"/>
	<mx:Button id="mySubmit" label="验证"/>  


3.PhoneNumberValidator 验证
类用于验证字符串是否为有效电话号码。有效电话号码包含至少 10 位数字,以及其它格式化字符。
该验证程序不检查电话号码是否为实际有效的电话号码。
<mx:PhoneNumberValidator source="{phone}" property="text" 
        trigger="{myButton}" triggerEvent="click"
        valid="Alert.show('Validation Succeeded!');"/>
 <mx:TextInput id="phone" width="100%"/>
 <mx:Button id="myButton" label="Validate" />


4.EmailValidator 验证
类用于验证字符串中是否有一个 @ 符号,域名中是否有句点,以及顶级域后缀中是否有两个、三个、四个或六个字符。
如果用方括号括住 IP 域名,则 IP 域名是有效的。验证程序不检查域名和用户名是否实际存在。 
<mx:EmailValidator 							
		id="emailV" 
		source="{txtEmail}" 
		property="text"
	    invalidCharError="非法字符"
	    invalidDomainError="非法域"
	    invalidIPDomainError="非法IP域"
	    missingAtSignError="缺少@符"
	    missingPeriodInDomainError="缺少域后缀"
	    missingUsernameError="缺少用户名"
		/>
<mx:TextInput x="74" y="175" width="117" id="txtEmail"/>	


5.自定义验证
package myCompenent
{
	import mx.validators.Validator;						//引用Validator类
    import mx.validators.ValidationResult;				//引用ValidationResult类
	public class myValidators extends Validator
	{
		public function myValidators()					//构造函数
		{
			super();
		}
		private var results:Array;						//定义一个数组,用以存储错误
		//重写验证函数
	    override protected function doValidation(value:Object):Array 
		 {     
            var s:String = value as String;
            results = [];								//清空数组
            results = super.doValidation(value);        //先用继承类中的doValidation方法验证 
            if (results.length > 0)						//如果验证时有错,返回错误信息
                return results;
            if(s.length>6)								//自定义验证,字符长度不超过6
            {
            	//记录出错信息
            	results.push(new ValidationResult(true,"text","StringTooLong","字符长度超过6了"));
            }        
            return results;
        }
	}
}

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:MyComp="myCompenent.*" fontSize="13">
    <MyComp:myValidators id="myVal" 
        source="{txtString}" property="text"   
        listener="{txtString}"  
        />    
    <mx:Panel title="自定义验证组件" width="300" height="200" horizontalAlign="center" verticalAlign="middle">
	    <mx:TextInput id="txtString"/>
	    <mx:Label text="{txtString.errorString}"/>
	    <mx:Button label="验证" click="myVal.validate();"/>    	
    </mx:Panel>
</mx:Application> 

你可能感兴趣的:(Validator)