0

I am trying to get character limit for input box ,but the input field has default value.

if character limit exceeds text has to appear at 0 limit(Not popup)

if i use below code it is not working fine for my requirement(say default value already exist).

can I know how to include default value and set message if it come to 0

class Company extends React.Component {
state = {
      Title: this.props.selectedTable.Title,
      chars_left: 50,
      max_char:50
    }

   handleCharacterCount= (event) => {
    const charCount = event.target.value.length;
    const maxChar = this.state.max_char;
    const charLength = maxChar - charCount;
    this.setState({ chars_left: charLength });
    //this.setState({sTitle: e.target.value});
   }
   render() {
    return (
     <div>
      <textArea
        type="text"
        maxLength="50"
        required
        onChange={this.handleCharacterCount}
        value={this.state.Title}/>
      />
      <p>{this.state.chars_left}</p>
    </div>
  )
 }
}
EQuimper
  • 5,811
  • 8
  • 29
  • 42
R9102
  • 687
  • 1
  • 9
  • 32

1 Answers1

1

You can try this

state = {
  Title: this.props.selectedTable.Title,
  chars_left: 50 - this.props.selectedTable.Title.length,
  max_char:50
}



handleCharacterCount= (event) => {
const charCount = event.target.value.length;
const maxChar = this.state.max_char;
const charLength = maxChar - charCount;
this.setState({ chars_left: charLength,Title: event.target.value })}



render() {
return (
 <div>
  <textArea
    type="text"
    maxLength="50"
    required
    onChange={this.handleCharacterCount}
    value={this.state.Title}/>
  />
  {
    !this.state.chars_left &&
      <p>text has to appear at 0 limit</p>
  }
  <p>{this.state.chars_left}</p>
</div>)}
Md Isfar Uddin
  • 692
  • 11
  • 19