html:
<input type="number" oninput="value=checkFormData(value,8,'float',2)">
js:
//检测输入框数据 value输入框值 length输入整数长度 type输入类型 point小数点后位数
function checkFormData(value,length ,type, point=2) {
if(value.length==0) return value;
if(value<0) value = Math.abs(value);
if(type=='text') {
if(value.length > length)value = value.slice(0, length);
} else if(type=='float'){
value = value.toString();
if(value.indexOf('.')>=0) {
let arr = value.split(".");
var val_int,val_float,p10=1;
if(arr[1].length>point ||arr[0].length>length) {
if(arr[0].length>length) {
val_int = arr[0].slice(0, length);
}else {
val_int = arr[0];
}
if(arr[1].length>point) {
val_float = arr[1].slice(0, point);
}else {
val_float = arr[1];
}
for (var i=0;i<point;i++) {
p10*=10;
}
val_float = parseFloat(val_float/p10);
value = parseFloat(parseInt(val_int) + parseFloat(val_float)).toFixed(point);
}
}else{
if(value.length > length)value = value.slice(0, length);
value = parseInt(value);
}
if(isNaN(value)) value = 0;
} else if(type=='int'){
if(value.length > length)value = value.slice(0, length);
value = parseInt(value);
if(isNaN(value)) value = 0;
}
return value;
}