如何仅在ExtJS的文本字段中允许最多3个小数位

问题描述

对于文本字段,我只需要允许其小数点后3位。

允许的输入:123、123.1、123.34、123.458,但不允许123.4563。

我尝试了很多文章,但最多只能找到2个小数位。

解决方法

我不知道您为什么要使用文本字段作为数字字段。无论如何,这是文本字段和数字字段的小提琴代码,精度为十进制3。

Ext.application({
    name: 'Fiddle',launch: function () {
        Ext.create('Ext.form.Panel',{
            title: "Form Panel",width: 300,renderTo: Ext.getBody(),layout: 'form',items: [{
                xtype: 'numberfield',name: 'bottles',fieldLabel: 'Number Field',decimalPrecision: 3 // This will allow only 3 decimal places.. 
            },{
                xtype: 'textfield',name: 'name',fieldLabel: 'Text Field',maskRe: /[0-9.-]/,//regex: /^-?[0-9]*(\.[0-9]{1,3})?$/,// Without error message
                validator: function (v) { // With error Message
                    return /^-?[0-9]*(\.[0-9]{1,3})?$/.test(v) ? true : 'Max. decimal precision is 3';
                },}],buttons: [{
                text: "Show Form Values",handler: function() {
                    console.log(this.up('form').getValues());
                }
            }]
        });
    }
});