T
T
testtoster2020-01-21 09:20:28
React
testtoster, 2020-01-21 09:20:28

How to set character input limit?

There is a field you which characters are entered. How to make it so that when the user enters less than 6 characters, he was given a message about this?

constructor(props) {
    super(props);
    this.showText = this.showText.bind(this);
    this.showText2 = this.showText2.bind(this);
  };

  showText2(e) {
    this.setState({ text2: e.target.value });
  }

  <input defaultValue={this.showText} onInput={this.showText2} />

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Suntsev, 2020-01-21
@GreyCrew

You don't even need react. Just add html5 attribute
UPD
And of course you can add an onBlur event

constructor(props) {
    super(props);
    this.showText = this.showText.bind(this);
    this.showText2 = this.showText2.bind(this);
  };

  showText2(e) {
    this.setState({ text2: e.target.value });
  }

  onFocusOff = (e) => {
    if(this.state.text2.length < 6){
      this.setState({ alert: "Не менее 6 символов" });
    }
  }

  <input defaultValue={this.showText} onBlur={this.onFocusOff} onInput={this.showText2} />
  {this.state.alert && <p>{this.state.alert}</p>}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question