flex 验证

validator1.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" fontSize="12">

    <mx:Script>
        import mx.controls.Alert;
       public function openUrl():void{
           navigateToURL(new URLRequest("http://hi.baidu.com/woaidelphi/"), "_blank");
       }  
    </mx:Script>
    <mx:Button label="查看更多flex" click="openUrl()"/>
   <mx:Panel title="验证大全" width="75%" height="75%"
        paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom="10" fontSize="12">

        <mx:Form>
            <mx:FormItem label="输入你的Email: ">
                <mx:TextInput id="email" width="100%"/>
            </mx:FormItem>
           
            <mx:FormItem label="请输入你的生日:(yyyy-mm-dd): ">
                <mx:TextInput id="dob" width="100%"/>
            </mx:FormItem>
           
            <mx:FormItem label="输入至少10位电话号码: ">
                <mx:TextInput id="phone" width="100%"/>
            </mx:FormItem>
           
            <mx:FormItem label="输入社会保险号(外国人用的如:444-44-4444): ">
                <mx:TextInput id="ssn" width="100%"/>
            </mx:FormItem>
           
            <mx:FormItem label="输入货币(可以有美元符$): ">
                 <mx:TextInput id="priceUS" width="100%"/>
            </mx:FormItem>
           
            <mx:FormItem label="输入一个数字(18-50): ">
                <mx:TextInput id="age" width="100%"/>
            </mx:FormItem>
           
            <mx:FormItem label="请输入4-20的字符: ">
                    <mx:TextInput id="fname" width="100%"/>
                </mx:FormItem>
           
            <mx:FormItem label="Enter a 5 or 9 digit U.S. 邮编号: ">
                <mx:TextInput id="zip" width="100%"/>
            </mx:FormItem>
           
            <mx:FormItem >
                <mx:Button id="myButton" label="确定" />
            </mx:FormItem>
        </mx:Form>
    </mx:Panel>
   

    <mx:EmailValidator source="{email}" property="text" trigger="{myButton}" triggerEvent="click" valid="Alert.show('Email格式正确!');"/>
    <mx:DateValidator source="{dob}" property="text" allowedFormatChars="-" inputFormat="yyyy-mm-dd" wrongMonthError="月份要在1-12之间."
trigger="{myButton}" triggerEvent="click" valid="Alert.show('出生日期格式正确!');"/>
    <mx:PhoneNumberValidator source="{phone}" allowedFormatChars="-+/" invalidCharError="格式不正确" wrongLengthError="数字太短了" property="text" trigger="{myButton}" triggerEvent="click" valid="Alert.show('电话格式正确!!');"/>
    <mx:SocialSecurityValidator source="{ssn}" property="text" trigger="{myButton}" triggerEvent="click" valid="Alert.show('社会保险号格式正确!!');"/>
    <mx:CurrencyValidator source="{priceUS}" property="text" precision="2" trigger="{myButton}" triggerEvent="click" valid="Alert.show('货币格式正确!');"/>
    <mx:NumberValidator source="{age}" property="text" integerError="请输入18-50之间的整数"
        minValue="18" maxValue="50" domain="int" trigger="{myButton}" triggerEvent="click" valid="Alert.show('数字验证正确!');"/>   
    <mx:StringValidator source="{fname}" property="text"
        tooShortError="字符串太短了,请输入最少4个字符. "
        tooLongError="字符串太长了,请输入最长20个字符. "
        minLength="4" maxLength="20"
        trigger="{myButton}" triggerEvent="click"
        valid="Alert.show('字符串格式正确!');"/>  
    <mx:ZipCodeValidator source="{zip}" property="text"
        trigger="{myButton}" triggerEvent="click"
        valid="Alert.show('邮编格式正确(外国人用的)');"/>
</mx:Application>

validator2.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white" creationComplete="initApp()">
<mx:Style>
.errorTip
{
fontSize: 12;
}
</mx:Style>
<mx:Script>
<![CDATA[
import mx.events.ValidationResultEvent;
import mx.controls.Alert;
import mx.validators.Validator;

private var myValidators:Array;

private function initApp():void {
myValidators = [valid1, valid2];
}

private function clickHandler():void {
var errors:Array = Validator.validateAll(myValidators);
if (errors.length == 0) {
Alert.show("Looks valid to me.", "SUCCESS");
}
}
]]>
</mx:Script>

<mx:StringValidator id="valid1" source="{ti1}" property="text" minLength="4" maxLength="6" tooShortError="太少了吧" tooLongError="太多了吧" />
<mx:StringValidator id="valid2" source="{ti2}" property="text" minLength="4" maxLength="6" />
<mx:Label text="最少4个字符,最多6个字符,谢谢!" fontSize="12"/>

<mx:TextInput id="ti1" />
<mx:TextInput id="ti2" />

<mx:Button label="validate" click="clickHandler()" />

</mx:Application>

 

 

    A+
发布日期:2009年05月14日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: