<input type="text" onblur="checkNum(this)"><script>functioncheckNum(obj){let val = obj.value.replace(/(^\s*)|(\s*$)/g,"")// 只能是负号,数字和小数点,不能是其他输入
val = val.replace(/[^\d.?!\-]/g,"")// 保证第一位不能是点
val = val.replace(/^\./g,"");// 负号只能出现一次
val = val.replace("-","$#$").replace(/\-/g,"").replace("$#$","-");// 负号不能在中间出现
val = val.replace(/(\d+)\-(\d*)/g,'$1$2');// 小数只能出现一次
val = val.replace(".","$#$").replace(/\./g,"").replace("$#$",".");// 小数点后面保留2位
val = val.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');// 重新赋值
obj.value = val
}</script>