Hello, I need to get the text of an input in react-native and check if this typed text has less than 11 characters, how could I do that check? I'm new with react
Hello, I need to get the text of an input in react-native and check if this typed text has less than 11 characters, how could I do that check? I'm new with react
(1) Where is the text stored? In the state of a component? If so, just check this.state.seuTexto.length
.
(2) When do you need to check? In the way you type or when you click a button?
(2.1) Pro first, you can use onChangeText
in a TextInput as follows:
onChangeText={text => {
if (text.length < 11) {
// tem menos de onze
}
// atualiza o estado
this.setState({ seuTexto: text });
}}
(2.2) To check after clicking a button, just check on onPress
onPress={() => {
if (this.state.seuTexto.length < 11) {
// menos de 11
}
}}
Note: (I advise you to extract the functions out of the render)