文本输入最小长度 React Native

Vin*_*y N 4 react-native react-native-ios react-native-textinput

有没有办法将文本输入限制在最小长度和最大长度之间。假设我想将文本输入长度限制在 5 到 15 之间,我该怎么做?

小智 6

考虑在您的组件中添加以下代码:

<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.');
   }
}
Run Code Online (Sandbox Code Playgroud)