簡體   English   中英

限制TextField充當數字步進器

[英]Restrict TextField to act like a numeric stepper

我正在從頭開始制作數字步進器,因此我希望我的文本字段僅接受以下格式的數字 :xx.x,xx,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;

您可以通過Flash復制過去,它應該可以工作。

非常感謝您的幫助,先生們。

與TheDarklins的答案非常相似,但更為優雅。 並實際上使_tf.restrict已過時,但我仍然建議使用它。

_tf.addEventListener(TextEvent.TEXT_INPUT, _onTextInput_validate);

這兩個事件偵聽器,在這里做同樣工作方式相同。 對於那些喜歡較小代碼的人,它是一行編寫的。 另一個是適合那些喜歡逐行查看情況的人。

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.現在在技術上有效。

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();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM