开发者

Restrict TextField to act like a numeric stepper

I am making a numeric stepper from scratch, so I want my text field to only accept numbers 开发者_运维问答in this format: xx.x, x.x, x, or xx where x is a number. For example: Acceptable numbers: 1 22 15.5 3.5

None Acceptable numbers: 213 33.15 4332 1.65

Maybe this will help some how: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/text/TextField.html#restrict

This is what I got so far:

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;

You can copy past this in flash and it should work.

Thank you very much for your help good sirs.


Very similar to TheDarklins answer, but a little more elegant. And actually renders _tf.restrict obsolete, but I would still recommend using it.

_tf.addEventListener(TextEvent.TEXT_INPUT, _onTextInput_validate);

Both of these event listeners here do the EXACT same function identically. One is written in a one line for those who like smaller code. The other is for those who like to see what's going on line by line.

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

for a more broken down version of the event listener

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

I have had to allow xx. as a valid response otherwise you would need to type 123 then go back a space and type . for 12.3. That is JUST NOT NICE. So 12. is now technically valid.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜