<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>实时过滤非数字(可包含两位小数点)的字符</title>
</head>
<body>
<input type="text" id="input">
</body>
<script type="text/javascript">
document.getElementById('input').addEventListener("input", inputFilter);
function inputFilter() {
let filterValue= this.value.replace(/^([0-9]\d{0,8})(\.\d{0,2})?|.*$/g, "$1$2")
this.value = filterValue.replace(/^(0)(\d)/g, '$2')
}
</script>
</html>
实时检测input输入框,仅支持数字,支持最多两位小数(最简单)
最新推荐文章于 2022-08-09 14:47:39 发布