this function does the following
what is not working are allowing negative numbers into the function
<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>
Your regex was nearly there, however it needed a few additions. Try this regex instead:
/^-?\d+(\.\d+)?$/
Your current implementation also didn't allow for, eg, 10.24, the above should work fine with that. If that was intentional, just change the \d+ to \d - this'll ensure that, if the user does enter a ., they at least have one number after.