I'm trying to show an alert when the user's input reaches six to twelve characters. I see that it only shows an alert if the character reaches 7.
Live editing here: https://snack.expo.dev/@vicrn101/66bbfe
Here's my code:
import React, {useEffect, useState} from "react";
import { Text, TextInput, View, StyleSheet } from 'react-native';
import Constants from 'expo-constants';
// You can import from local files
import AssetExample from './components/AssetExample';
// or any pure javascript modules available in npm
import { Card } from 'react-native-paper';
export default function App() {
const [moreThanSix, setMoreThanSix] = useState(false);
const validateEntry = (textEntry) => {
console.log('textEntry: ', textEntry);
console.log('length: ', textEntry.length);
if(textEntry.length > 5 && textEntry.length < 13) {
console.log('moreThanSix: ', textEntry.length);
setMoreThanSix(true);
} else {
setMoreThanSix(false)
}
console.log('moreThanSix: ', moreThanSix);
if(moreThanSix) {
alert('SixToTwelveCharacters: ', moreThanSix);
}
}
return (
<View style={styles.container}>
<Text style={styles.paragraph}>
Show alert when text length is 6 to 12 characters.
</Text>
<Card>
<Text>Is six to twelve characters: {moreThanSix}</Text>
<TextInput
onChangeText={(textEntry) => validateEntry(textEntry)}
/>
</Card>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
},
paragraph: {
margin: 24,
fontSize: 18,
fontWeight: 'bold',
textAlign: 'center',
},
});
Live editing here: https://snack.expo.dev/@vicrn101/66bbfe