【发布时间】:2021-12-07 20:49:36
【问题描述】:
此函数执行以下操作
- 不允许使用字符
- 允许退格
- 有上限
- 有一个下限
- 最小步长为 0.1,即没有小于 0.1 的数字,即不是 0.01
什么不工作是允许负数进入函数
<html>
<input id="setPoint" type="text" name="setPoint" onkeydown="myFunction(event,-80, 150)" />
<script>
function myFunction(e, low, high) {
console.log("low" + low);
console.log("high" + high);
var nextValue = e.target.value + e.key;
console.log("NEXT VALUE:" + nextValue);
if (e.which == 8) { // allow backspace
return;
}
if (!/^(\d+)?([.]?\d{0,1})?$/.test(nextValue)) {
e.preventDefault(); // non-number, don't allow
}
if (nextValue > high) {
e.preventDefault();
}
if(nextValue< low){
e.preventDefault();
}
}
</script>
</html>
【问题讨论】:
-
@Spectric 你能否修改你的正则表达式以允许负数
标签: javascript html forms input