Actionscript 仅限制具有两位小数的数字

Yan*_*Yan 1 regex flash actionscript actionscript-2

为什么不简单!

我只想限制输入文本仅允许带有 2 位小数的数字示例 22.44 10.55 6.00 55.72

我怎样才能用actionscript 2.0做到这一点?

祝福会加薪的帮手!

wel*_*rat 5

将可以输入文本字段的字符限制为数字 0 到 9 和小数点:

textField.restrict = "0-9.";
Run Code Online (Sandbox Code Playgroud)

然后向 onChanged 事件添加一个侦听器函数,删除小数点后两个字符以外的所有内容,或任何第二次出现的“.”:

textField.onChanged = function () {
    var ind = textField.text.indexOf (".");
    if ( ind > -1) {
        var decimal = textField.text.substring (ind+1);
        if (decimal.indexOf (".") > -1) {
            textField.text = textField.text.substring (0, ind + 1 + decimal.indexOf("."));
        }
        if (decimal.length > 2) {
            textField.text = textField.text.substring (0, ind + 3);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)