限制 TextField 充当数字步进器

发布于 2024-10-05 04:28:50 字数 643 浏览 4 评论 0原文

我正在从头开始制作数字步进器,因此我希望我的文本字段仅接受以下格式的数字: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 中复制此内容,它应该可以工作。

非常感谢各位好心先生的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蛮可爱 2024-10-12 04:28:50

与 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. 现在在技术上是有效的。

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.

叫嚣ゝ 2024-10-12 04:28:50
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();
        }
    }
}
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();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文