I have an input with React, but maxlength
does not work. Does anyone know how to solve this?
This is handleChangeInput
handleChangeInput(input) { this.setState({ ...this.state, form: { ...this.state.form, [input.target.name]: input.target.value } }) }
And this is my input:
<div className="input-field col s12 m6 l6"> <input onChange={this.handleChangeInput} value={this.state.form.message} type="text" className="phone validate" name="phone" maxlength="11"/> <label for="telefone">Telefone</label> </div>
js # Use the maxLength prop to set a character limit on an input field in React, e.g. <input maxLength={5} /> . The maxLength attribute defines the maximum number of characters the user can enter into an input field or a textarea element.
The maxlength attribute defines the maximum number of characters (as UTF-16 code units) the user can enter into an <input> or <textarea> . This must be an integer value 0 or higher. If no maxlength is specified, or an invalid value is specified, the input or textarea has no maximum length.
I have an input with React, but maxlength does not work. Does anyone know how to solve this? Show activity on this post. Property and attribute names are generally camelCase in React, maxLength works.
Property and attribute names are generally camelCase in React, maxLength works. However, you can still override this option if you give the input a value longer than maxLength. The only way around this is to check the length of the value in the callback, and truncate it.
The maxLength prop sets the value of the maxlength attribute of the input. Also, we set the type attribute of the input to text to make sure maxlength is enforced. Then we set the onChange and value props to the event handler function and value respectively. Now we should see that we can’t enter more than 10 characters into the input.
Thanks for trying, but it didn't work. If you want the max length for a string use type=text and maxLenght. But if you want the max number, you should use type=number with max. A community for learning and developing web applications using React by Facebook.
Property and attribute names are generally camelCase
in React, maxLength
works.
<input onChange={this.handleChangeInput} value={this.state.form.message} type="text" className="phone validate" name="phone" maxLength="11" />
However, you can still override this option if you give the input a value
longer than maxLength
. The only way around this is to check the length of the value
in the callback, and truncate it.
Example
class App extends React.Component { state = { form: { message: "" } }; handleChangeInput = event => { const { value, maxLength } = event.target; const message = value.slice(0, maxLength); this.setState({ form: { message } }); }; render() { return ( <input onChange={this.handleChangeInput} value={this.state.form.message} type="text" className="phone validate" name="phone" maxLength="11" /> ); } }
for maxLength to work, type has to be 'text' (most people are probably putting number)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With