int
is not a built-in function for the current version of JavaScript. The syntax is parseInt(value)
;
Read about parseInt
here -> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
You can use the oninput
event to detect when the value changes. This is where you check the value and adjust, if needed. However, it's important to note that once you reach the max or min value, the arrows on the control will not fire this event. So you can't just roll it over from MAX_VALUE to MIN_VALUE using the arrow keys.
Read more here -> What events does an <input type="number" /> fire when it's value is changed?
Putting this all together...
HTML
<input type="number" id="customer-age-input" max="120" min="0" oninput="checkValue(this);" />
JavaScript
// this function will convert a string to an integer
// beware this will throw an exception if the value does not parse properly
function int(value) {
return parseInt(value);
}
// this checks the value and updates it on the control, if needed
function checkValue(sender) {
let min = sender.min;
let max = sender.max;
let value = int(sender.value);
if (value>max) {
sender.value = min;
} else if (value<min) {
sender.value = max;
}
}
I've only created the int function out of courtesy of your design. You can just use parseInt in place of the customized int function in the code above. The new code would look something like:
JavaScript
// this checks the value and updates it on the control, if needed
function checkValue(sender) {
let min = sender.min;
let max = sender.max;
// here we perform the parsing instead of calling another function
let value = parseInt(sender.value);
if (value>max) {
sender.value = min;
} else if (value<min) {
sender.value = max;
}
}