React Js disable button according to input length
React Js disable button according to input length:To disable a button in React.js based on input length, you can use the disabled
attribute in the button element and control it dynamically using state. First, create a state variable to store the input value.
Then, use an onChange
event handler on the input field to update the state with the current input value. Finally, set the disabled
attribute of the button to true
if the input length is zero or exceeds a certain limit, and false
otherwise. By doing so, the button will automatically disable or enable based on the length of the input.
Thanks for your feedback!
Your contributions will help us to improve service.
How can I disable a button in Reactjs based on the length of the input?
The provided React.js code creates a form with two input fields for the first name and last name. It also includes a button labeled "Add." The button is disabled by default and will be enabled only when both the first name and last name input fields have a length greater than or equal to 3 characters.
This behavior is achieved using the useState
and useEffect
hooks. When the button is clicked, the addNewCustomer
function is called, passing the first name and last name as parameters.