【问题标题】:Restrict TextField to act like a numeric stepper限制 TextField 像数字步进器一样工作
【发布时间】:2011-05-18 22:27:18
【问题描述】:

我正在从头开始制作数字步进器,因此我希望我的文本字段只接受以下格式的数字:xx.x、x.x、x 或 xx,其中 x 是数字。例如: 可接受的数字: 1 22 15.5 3.5

无 可接受的数字: 213 33.15 4332 1.65

也许这会有所帮助: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/text/TextField.html#restrict

这是我目前得到的:

var tx:TextField = new TextField();
tx.restrict="0-9."; //Maybe there is a regular expression string for this?
tx.type=TextFieldType.INPUT;
tx.border=true;

您可以在闪存中复制过去,它应该可以工作。

非常感谢各位大佬的帮助。

【问题讨论】:

    标签: regex apache-flex flash actionscript-3 textfield


    【解决方案1】:
    package
    {
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFieldType;
    import flash.events.TextEvent;
    
    public class DecimalPlaces extends Sprite
        {
        public function DecimalPlaces()
            {
            var tf:TextField = new TextField();
            tf.type = TextFieldType.INPUT;
            tf.border = true;
            tf.width = 200;
            tf.height = 16;
            tf.x = tf.y = 20;
            tf.restrict = ".0-9"
            tf.addEventListener(TextEvent.TEXT_INPUT, restrictDecimalPlaces);
    
            addChild(tf);
            }
    
        function restrictDecimalPlaces(evt:TextEvent):void
            {
            var matches:Array = evt.currentTarget.text.match(/\./g);
            var allowedDecimalPlaces:uint = 1;
    
            if  ((evt.text == "." && matches.length >= 1) ||
                (matches.length == 1 && (evt.currentTarget.text.lastIndexOf(".") + allowedDecimalPlaces < evt.currentTarget.text.length)))
                evt.preventDefault();
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      与 TheDarklins 的答案非常相似,但更优雅一些。实际上会使_tf.restrict 过时,但我仍然建议使用它。

      _tf.addEventListener(TextEvent.TEXT_INPUT, _onTextInput_validate);
      

      这里的这两个事件侦听器都执行相同的EXACT功能。对于那些喜欢较小代码的人来说,一个是用一行编写的。另一种是为那些喜欢逐行查看正在发生的事情的人。

      private function _onTextInput_validate(__e:TextEvent):void
      {
          if ( !/^\d{1,2}(?:\.(?:\d)?)?$/.test(TextField(__e.currentTarget).text.substring(0, TextField(__e.currentTarget).selectionBeginIndex) + __e.text + TextField(__e.currentTarget).text.substring(TextField(__e.currentTarget).selectionEndIndex)) ) __e.preventDefault();
      }
      

      对于事件监听器的更细分版本

      private function _onTextInput_validate(__e:TextEvent):void
      {
          var __reg:RegExp;
          var __tf:TextField;
          var __text:String;
      
          // set the textfield thats causing the event.
          __tf = TextField(__e.currentTarget);
      
          // Set the regular expression.
          __reg = new RegExp("\\d{1,2}(?:\\.(?:\\d)?)?$"); 
          // or depending on how you like to write it.
          __reg = /^\d{1,2}(?:\.(?:\d)?)?$/;
      
          // Set all text before the selection.
          __text = __tf.text.substring(0, __tf.selectionBeginIndex);
          // Set the text entered.
          __text += __e.text;
          // Set the text After the selection, since the entered text will replace any selected text that may be entered
          __text += __tf.text.substring(__tf.selectionEndIndex);
      
          // If test fails, prevent default
          if ( !__reg.test(__text) )
          {
              __e.preventDefault();
          }
      }
      

      我必须允许 xx. 作为有效响应,否则您需要输入 123 然后返回一个空格并输入 。 12.3。那不是很好。所以12. 现在在技术上是有效的。

      【讨论】:

      • 似乎删除或退格键不会触发TEXT_INPUTCHANGE 事件。我曾尝试在按下的删除键上preventdefault,但我没有成功。我不知道如何防止用户输入 12.3 然后删除 . 使其无效。 :( 对不起。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多