flex_内置验证器集合示例;

来源:互联网 发布:贵州数据铁笼 编辑:程序博客网 时间:2024/04/30 00:13
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               minWidth="1024" minHeight="768" pageTitle="TheStudioOfCenyebao">
    <fx:Script>
        <![CDATA[
            import mx.events.ValidationResultEvent;
            import mx.validators.RegExpValidationResult;
            
            protected function rgvalidator_validHandler(event:ValidationResultEvent):void
            {
                var theResult:RegExpValidationResult;
                for(var i:int = 0; i<event.results.length; i++) {
                    theResult = event.results[i];
                    trace("=1>Found a match string at Index:" + theResult.matchedIndex+"\n=2 >On the characters" +
                        "of: "+theResult.matchedString);
                }
            }
            
        ]]>
    </fx:Script>

    <fx:Declarations>
        <!-- 非可视元素 -->
        <mx:Validator source="{username}" property="text" required="true" requiredFieldError="请输入您的用户名!"/>
        
        <mx:StringValidator source="{pwdStr}" property="text" required="false" minLength="3" tooShortError="不能少于3个字节!"
                            maxLength="8" tooLongError="不能多于8个字节!" trigger="{submitBtn}" triggerEvent="click"/>
        
        <mx:NumberValidator source="{age}" property="text" allowNegative="false" negativeError="不允许为负数!"
                            minValue="5" maxValue="120" domain="int"/>
        
        <!--mx:DateValidator source="{birthday}" property="text" inputFormat="mm/dd/yyyy" allowedFormatChars="/"/-->
        <mx:DateValidator source="{birthday}" property="text" inputFormat="yyyy-mm-dd" allowedFormatChars="-"/>
        <mx:DateValidator yearSource="{yeartxt}" yearProperty="text" monthSource="{monthfield}" monthProperty="value"
                          daySource="{dayfield}" dayProperty="value" trigger="{dateBtn}" triggerEvent="click"/>
        
        <mx:EmailValidator source="{email}" property="text" invalidCharError="存在无效字符!"/>
        
        <mx:CreditCardValidator cardNumberSource="{cardNumber}" cardNumberProperty="text"
                                cardTypeSource="{cardType}" cardTypeProperty="selectedItem"
                                trigger="{cardBtn}" triggerEvent="click"/>
        
        <mx:CurrencyValidator source="{assets}" property="text" allowNegative="false" negativeError="不允许为负!"
                              precision="2" precisionError="只能有两位小数!"/>
        
        <mx:PhoneNumberValidator source="{phoneNum}" property="text"/>
        
        <mx:RegExpValidator source="{personId}" property="text" flags="gmi"
                            expression="^[0-9A-Z]\{18\}$" noMatchError="格式不匹配!"/>
        
        <mx:RegExpValidator id="rgvalidator" source="{test}" property="text" expression="m[ai]n" flags="gmi"
                            valid="rgvalidator_validHandler(event)"    noMatchError="应该包含m[ai]n格式字符串!"
                            trigger="{testBtn}"    triggerEvent="click"/>
        
    </fx:Declarations>
    
    <s:layout>
        <s:VerticalLayout gap="20" paddingLeft="20" paddingTop="20"/>
    </s:layout>
    
    <!--view-->
    <s:HGroup gap="20">
        <s:VGroup>
            <s:Label text="Validator" fontWeight="bold" fontSize="18"/>
            <s:Label text="用户名:" fontWeight="bold" fontSize="16"/>
            <s:TextInput id="username"/>
        </s:VGroup>
        
        <s:VGroup>
            <s:Label text="StringValidator" fontWeight="bold" fontSize="18"/>
            <s:Label text="登录密码:" fontWeight="bold" fontSize="16"/>
            <s:TextInput id="pwdStr"/>
            <s:Button label="验证" id="submitBtn"/>
        </s:VGroup>
        
        <s:VGroup>
            <s:Label text="NumberValidator" fontWeight="bold" fontSize="18"/>
            <s:Label text="年龄:" fontWeight="bold" fontSize="16"/>
            <s:TextInput id="age"/>
        </s:VGroup>
        
        <s:VGroup>
            <s:Label text="DateValidator" fontWeight="bold" fontSize="18"/>
            <s:Label text="出生日期:" fontWeight="bold" fontSize="16"/>
            <s:TextInput id="birthday"/>
            <s:HGroup width="100%">
                <s:TextInput id="yeartxt" width="60"/><s:Label text="年" fontWeight="bold" fontSize="16"/>
                <s:NumericStepper id="monthfield" maximum="12"/><s:Label text="月" fontWeight="bold" fontSize="16"/>
                <s:NumericStepper id="dayfield" maximum="31"/><s:Label text="日" fontWeight="bold" fontSize="16"/>
                <s:Button label="日期验证" id="dateBtn"/>
            </s:HGroup>
        </s:VGroup>
        
        <s:VGroup>
            <s:Label text="EmailValidator" fontWeight="bold" fontSize="18"/>
            <s:Label text="邮箱地址:" fontWeight="bold" fontSize="16"/>
            <s:TextInput id="email"/>
        </s:VGroup>
    </s:HGroup>
    
    <s:HGroup gap="20">
        <s:VGroup>
            <s:Label text="CreditCardValidator" fontWeight="bold" fontSize="18"/>
            <s:DropDownList id="cardType" width="150" prompt="请选择信用卡类型。。。">
                <mx:ArrayCollection>
                    <fx:String>American Express</fx:String>
                    <fx:String>Visa</fx:String>
                    <fx:String>Diners Club</fx:String>
                    <fx:String>Discover</fx:String>
                    <fx:String>MasterCard</fx:String>
                </mx:ArrayCollection>
            </s:DropDownList>
            <s:Label text="卡号:" fontWeight="bold" fontSize="16"/>
            <s:TextInput id="cardNumber" width="150"/>
            <s:Button id="cardBtn" label="信用卡验证"/>
        </s:VGroup>
        
        <s:VGroup>
            <s:Label text="CurrencyValidator" fontWeight="bold" fontSize="18"/>
            <s:Label text="资产:" fontWeight="bold" fontSize="16"/>
            <s:TextInput id="assets"/>
        </s:VGroup>
        
        <s:VGroup>
            <s:Label text="PhoneNumberValidator" fontWeight="bold" fontSize="18"/>
            <s:Label text="手机号码:"/>
            <s:TextInput id="phoneNum"/>
        </s:VGroup>
        
        <s:VGroup>
            <s:Label text="RegExpValidator(1)" fontWeight="bold" fontSize="18"/>
            <s:Label text="身份证号:"/>
            <s:TextInput id="personId" width="150"/>
        </s:VGroup>
        
        <s:VGroup>
            <s:Label text="RegExpValidator(2)" fontWeight="bold" fontSize="18"/>
            <s:Label text="所有匹配:"/>
            <s:TextInput id="test" width="150" text="many many people"/>
            <s:Button id="testBtn" label="检测"/>
        </s:VGroup>
    </s:HGroup>
    
</s:Application>


原创粉丝点击