Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maxlength does not work React Js

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> 

Code preview

like image 281
MKOT Avatar asked Jul 03 '18 19:07

MKOT


People also ask

How do you set maxLength in React?

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.

Is maxLength an attribute?

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.

Does maxLength work with react input?

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.

How do I override maxLength in react?

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.

How do I set the maxLength of the input?

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.

How to get the max length of a string in JavaScript?

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.


2 Answers

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"       />     );   } } 
like image 80
Tholle Avatar answered Oct 02 '22 11:10

Tholle


for maxLength to work, type has to be 'text' (most people are probably putting number)

like image 26
Micah Friedland Avatar answered Oct 02 '22 11:10

Micah Friedland