0

I have created a simple form. I need to know how to validate email and password with minimum of 8 characters. many tutorials are designed for the class components. But I couldn't able to handle them with the functional components. please help me

const SigninPage = ({navigation}) => {
      const [email, setEmail] = useState('');
      const [password, setPassword] = useState('');
      const {colors} = useTheme();
    

return(
 <View style={styles.inputView}>
          <TextInput
            style={styles.TextInput}
            placeholder="E-mail"
            placeholderTextColor="white"
            onChangeText={(email) => setEmail(email)}
          />
        </View>

        <View style={styles.inputView}>
          <TextInput
            style={styles.TextInput}
            placeholder="Password"
            placeholderTextColor="white"
            secureTextEntry={true}
            onChangeText={(password) => setPassword(password)}
          />
        </View>

        <TouchableOpacity
          onPress={() => signIn() }>
          <LinearGradient colors={['#730018', '#00085b']} style={styles.signIn}>
            <Text style={styles.textSign}>SIGN IN</Text>
          </LinearGradient>
        </TouchableOpacity>
        <Text>Fogot Your Password?</Text>
      </Animatable.View>
    </View>
)

}

1 Answer 1

3

You are almost done dear!

See, I have slightly modified your code.

const SigninPage = ({ navigation }) => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const { colors } = useTheme();

const signIn = () => {                          // <= Added this function
    const strongRegex = new RegExp("^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$");

    if (!strongRegex.test(email)) {
        showMessage(MESSAGE.email)
        return false;
    } else if (password.length < 8) {
        showMessage(MESSAGE.password);
        return false;
    }
}

return (
    <View>                                      
        <Animatable.View >
            <View style={styles.inputView}>
                <TextInput
                    style={styles.TextInput}
                    placeholder="E-mail"
                    placeholderTextColor="white"
                    onChangeText={(email) => setEmail(email)}
                />
            </View>
            <View style={styles.inputView}>
                <TextInput
                    style={styles.TextInput}
                    placeholder="Password"
                    placeholderTextColor="white"
                    secureTextEntry={true}
                    onChangeText={(password) => setPassword(password)}
                />
            </View>

            <TouchableOpacity
                onPress={() => signIn()}>
                <LinearGradient colors={['#730018', '#00085b']} style={styles.signIn}>
                    <Text style={styles.textSign}>SIGN IN</Text>
                </LinearGradient>
            </TouchableOpacity>
            <Text>Fogot Your Password?</Text>
        </Animatable.View >
    </View>
)

}

I hope you need the same thing.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Dhruv ! Could you please explain and give how can I include email validator here? Because I coouldn't able to realize that one
Thank you so much, Dhruv! You made my task easier!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.