Flex的 TextInput 限制有小数点和负号的数字



 http://bbs.9ria.com/thread-86454-1-1.html



package
{
    import flashx.textLayout.operations.InsertTextOperation;
   
    import spark.components.TextInput;
    import spark.events.TextOperationEvent;
   
    [Exclude(name="restrict", kind="property")]
   
    public class NumberInput extends TextInput
    {
        public function NumberInput()
        {
            updateRegex();
        }
      
        //regex pattern
        private var regex:RegExp;
      
        private var _allowNegative:Boolean = true;
        /**
         * Specifies whether negative numbers are permitted.
         * Valid values are true or false.
         *
         * @default true
         */
        public function set allowNegative(value:Boolean):void
        {
            _allowNegative = value;
            updateRegex();
        }
      
        private var _fractionalDigits:int = 0;
        /**
         * The maximum number of digits that can appear after the decimal
         * separator.
         *
         *
        
         The default value is 0
         */
        public function set fractionalDigits(value:int):void
        {
            _fractionalDigits = value;
            updateRegex();
        }
      
        /**
         *  @private
         */
        override public function set restrict(value:String):void
        {
            throw(new Error("You are not allowed to change the restrict property of this class.  It is read-only."));
        }
      
        override protected function childrenCreated():void
        {
            super.childrenCreated();
          
            //listen for the text change event
            addEventListener(TextOperationEvent.CHANGING, onTextChange);
        }
      
        public function onTextChange(event:TextOperationEvent):void
        {
            if (regex && event.operation is InsertTextOperation)
            {
                // What will be the text if this input is allowed to happen
                var textToBe:String = "";
                // Check selection
                if (selectionActivePosition > 0)
                {
                    textToBe += text.substr(0, selectionActivePosition)
                }
                //append the newly entered text with the existing text
                textToBe += InsertTextOperation(event.operation).text;
                if (selectionAnchorPosition > 0)
                {
                    textToBe += text.substr(selectionAnchorPosition, text.length - selectionAnchorPosition);
                }
                var match:Object = regex.exec(textToBe);
                if (!match || match[0] != textToBe)
                {
                    // The textToBe didn't match the expression... stop the event
                    event.preventDefault();
                }
                //special condition checking to prevent multiple dots
                var firstDotIndex:int = textToBe.indexOf(".");
                if( firstDotIndex != -1)
                {
                    var lastDotIndex:int = textToBe.lastIndexOf(".");
                    if(lastDotIndex != -1 && lastDotIndex != firstDotIndex)
                        event.preventDefault();
                }
            }
        }
      
        private function updateRegex():void
        {
            var regexString:String = "\\d{0,50}";
            if(_allowNegative)
                regexString = "^\\-?" + regexString;
            else
                regexString = "^" + regexString;
            if(_fractionalDigits > 0 )
                regexString += "\\.?\\d{0," + _fractionalDigits.toString() + "}$";
            else
                regexString += "$";
            regex = new RegExp(regexString);
        }
    }
}




原文链接:http://cookbooks.adobe.com/post_Flex_TextInput_restrict_numbers_with_decimals_and-18997.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值