So, I have 3 input field and I want to control just one input’s value. The input that I want to control should be bigger or equal the first input’s value and it should be lesser or equal the second input’s value. The issue is that when i try to get value of the input with onChange func it can get only 1 character once.
here is the input that i want to controll
<input
className={cn(styles.textField)}
name="floorsTo"
value={inputVal}
onChange={(e) => {
e.persist();
const { value } = e.target;
if (
parseInt(value) > parseInt(floorsCount) ||
parseInt(value) < parseInt(floorsFrom)
) {
return false;
}
setInputValue(value);
}}
type="text"
/>
and floorsCount and floorsFrom are the other inputs. by the way it works for only single numbers
2
Answers
It’s not working because in every change you’re checking the value against the other two values. You can make this check at onBlur because you need to let the user enter the complete value, first. For example, if the first number is 10 and second one is 20, the user tries to enter 15 but the logic above doesn’t let them enter 1 because 1 is less than 10.
You can make the check at onBlur and if it passes, you can use the new value and if not you can return back to the latest value that input had.
As far as I understood your question. You need to have the controlled input values to be ranged in between the first input controls specified number and the second input controls specified number.
Refer the code below for reference: