只允许输入数字(整数:小数点不能输入)
<input type="text" onkeyup="value=value.replace(/[^\d]/g,'')" >
允许输入小数(两位小数)
<input type="text" onkeyup="value=value.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')" >
允许输入小数(一位小数)
<input type="text" onkeyup="value=value.replace(/^\D*(\d*(?:\.\d{0,1})?).*$/g, '$1')" >
开头不能为0,且不能输入小数
<input type="text" onkeyup="value=value.replace(/[^\d]/g,'').replace(/^0{1,}/g,'')" >
只能输入数字(包括小数)
<input type="text" oninput ="value=value.replace(/[^0-9.]/g,'')" >
//1、保留小数点后3位切四舍五入
getFloat(num, n) {
n = n ? parseInt(n) : 0
if (n <= 0) {
return Math.round(num)
}
num = Math.round(num * Math.pow(10, n)) / Math.pow(10, n) //四舍五入
num = Number(num).toFixed(n) //补足位数
return num
},
const num1 = this.getFloat(1.23467, 3)
console.log(num1) //1.235
//2、保留小数点后3位
const num2 = String(1.23467).replace(/^(.*\..{3}).*$/, '$1')
console.log(num2) //1.234
input输入限制
最新推荐文章于 2024-01-10 10:15:08 发布