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/