extjs - 如何限制数字字段输入超过两位小数

标签 extjs extjs4.1 extjs4.2

It seems very easy to force the decimal to some precision and their are many ways.I have tried using all as:

第一种方法:使用配置

            xtype: 'numberfield',
            fieldLabel: 'Balance',
            name: 'balancefield',    
            decimalPrecision:2

替代方案:通过覆盖或扩展,如图所示here

或者使用正则表达式

All of these work on blur or some other event or function.Is their a way to stop the input after two decimals. In textfields we can use RegEx and maskRe configs.But in numberfield,is their anything like maskRe,so that user cannot enter the more than two decimals. Any help is appreciated.Thanks.

最佳答案

maskRe 确实由数字字段在内部使用,因此您必须模仿它的行为。也就是说,监听字段的 keypress 事件,如果输入适合您,则停止该事件。

示例,使用正则表达式强制两位小数精度:

Ext.widget('numberfield', {
    renderTo: Ext.getBody()
    ,enableKeyEvents: true
    ,listeners: {
        keypress: function(field, e) {
            var v = field.getValue();
            if (!Ext.isEmpty(v)) {
                if (/\.\d{2,}/.test(v)) {
                    e.stopEvent();
                }
            }
        }
    }
});

关于extjs - 如何限制数字字段输入超过两位小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16911780/

相关文章:

javascript - ExtJS:容器默认样式从何而来?

extjs - 在 ext js 中隐藏操作列项

javascript - ExtJS Controller 中未调用 onLaunch 函数

javascript - 使用 filterBy() 应用过滤器时从 ExtJs 存储中清除过滤器的最快方法

javascript - 使用 EXTjs 预先加载组合框的项目

javascript - 如何在 ExtJ 中深入分析图表

javascript - EXTJS 日期字段验证

javascript - ExtJS Ajax POST 与代理 POST

css - ExtJS 4.2 为菜单添加框架,风格不同于面板

extjs - extjs中http post时的分页参数