extjs - 如何仅在 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());
}
}]
});
}
});
推荐阅读
- python - 返回目录中文件的最大形状的最佳方法?
- database - 如何在概念用户电子邮件和身份验证令牌之间进行映射?
- java - 如何在我的单元测试中测试方法?
- python - 如何将两个不同的数据集合并或连接成一个
- c# - OAuth 1.0 不断给我一个无效的签名方法
- swift - 如何从函数中获取更改后的视图控制器的值?
- css - 如何将基本字体大小覆盖为 62.5%?
- java - 没有找到匹配的方法创建为类 java.lang.Class 使用 2 个参数
- python - matplotlib 中出现意外的关键字参数
- python - Flask:如何修复 Mysql 操作错误 2013?