I have a simple number input in react component. I need to make it not accept more than 4 characters. But it doesn’t work with maxLength attribute. What else i can do to prevent it taking more than 4 characters.
<input type="number" onchange={handleChange}
<input type="number"/>
/>
2
Answers
you can add readonly attribute to the input when your hook gets 4 characters
The handleChange function checks if the length of the input value is less than or equal to 4 before updating the state.
If you wanna play around with the code, here is the sandbox.