仅个人经验,希望能帮到有需要的人。
第一次写 就话不多说了直接上代码。
// 只能输入数字且只有一位小数
proving(e) {
// this.form.skus[e].Price 是input的值
// 先把非数字的都替换掉,除了数字和.
this.form.skus[e].Price = this.form.skus[e].Price.replace(/[^\d.]/g, '');
// 必须保证第一个为数字而不是.
this.form.skus[e].Price = this.form.skus[e]Price.replace(/^\./g, '');
// 保证只有出现一个.而没有多个.
this.form.skus[e].Price = this.form.skus[e].Price.replace(/\.{2,}/g, '');
// 保证.只出现一次,而不能出现两次以上
this.form.skus[e].Price = this.form.skus[e].Price.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.');
let index = -1
for (let i in this.form.skus[e].Price) {
if (this.form.skus[e].Price[i] === '.') {
index = i
}
if (index !== -1) {
if (i - index > 1) {
this.form.skus[e].Price = this.form.skus[e].Price.substring(0, this.form.skus[e].Price.length - 1)
}
}
}
},
input只能输入数字或两位小数
/** * [只能输入数字和两位小数] * 举例:
input框限制只能输入正整数、字母、小数、汉字
有时需要限制文本框输入内容的类型,本节分享下正则表达式限制文本框只能输入数字.小数点.英文字母.汉字等代码. 例如,输入大于0的正整数 代码如下: funct ...
jquery控制input只能输入数字和两位小数
jquery代码 function num(obj){ obj.value = obj.value.replace(/[^\d.]/g,""); //清除"数字" ...
Delphi中限制文本框(TEdit)只能输入数字
procedure Tform1.Edit1KeyPress(Sender: TObject; var Key: Char);var edt: TEdit; str, strL, strR: stri ...
[转]JS 只能输入数字和两位小数的JS