【发布时间】:2023-05-04 19:20:02
【问题描述】:
下面是我到目前为止的内容,问题是它适用于输入和粘贴,但如果你再次粘贴,它看起来只适用于从粘贴操作复制的数据,它没有考虑到已经存在的内容在那个领域。
$('#val00').on('input', function(){
this.value = this.value
.replace(/[^\d.]/g, '') // numbers and decimals only
.replace(/(\..*)\./g, '$1') // decimal can't exist more than once
.replace(/(\.[\d]{2})./g, '$1'); // not more than 2 digits after decimal
console.log("ON INPUT "+mynum);
mynum++;
})
$('#val00').on('paste', function(){
this.value = this.value
.replace(/[^\d.]/g, '') // numbers and decimals only
.replace(/(\..*)\./g, '$1') // decimal can't exist more than once
.replace(/(\.[\d]{2})./g, '$1'); // not more than 2 digits after decimal
console.log("ON PASTE "+mynum);
mynum++;
})
【问题讨论】:
标签: javascript jquery html validation input