Textinput minimum length React native

Vinay N picture Vinay N · Feb 19, 2019 · Viewed 15.3k times · Source

Is there a way to limit the textinput between a minimum length and maximum length. Suppose I want to limit the textinput length between 5 and 15, how do I do that ?

Answer

arjayosma picture arjayosma · Feb 19, 2019

Consider adding the following code in your component:

<TextInput onTextChange={this.onTextChange} maxLength={15} ... />
<Button onPress={this.onPress} ... >Submit</Button>

onTextChange = text => {
   this.setState({text : text});
}

onPress = () => {
   const {text} = this.state;

   if(text.length < 5) {
      console.log('Your text is less than what is required.');
   }
}