Commit fabccd90 authored by W.D.R.P. Sandeepa's avatar W.D.R.P. Sandeepa

validation

parent f401a523
import { useNavigation } from "@react-navigation/native"; import { useNavigation } from "@react-navigation/native";
import Orientation from 'react-native-orientation-locker'; import Orientation from 'react-native-orientation-locker';
import React from "react"; import React, { useState } from "react";
import { SafeAreaView, ScrollView, View, StyleSheet, ImageBackground, Text, TextInput, TouchableOpacity, Image } from "react-native"; import { SafeAreaView, ScrollView, View, StyleSheet, ImageBackground, Text, TextInput, TouchableOpacity, Image } from "react-native";
const isValidObjectField = (obj) => {
return Object.values(obj).every(value => value.trim());
}
const updateError = (error, stateUpdater) => {
stateUpdater(error);
setTimeout( () => {
stateUpdater('')
}, 2500);
}
const isValidEmail = (value) => {
const regex = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;
return regex.test(value);
}
const Login = () => { const Login = () => {
const navigation = useNavigation(); const navigation = useNavigation();
...@@ -15,6 +31,46 @@ const Login = () => { ...@@ -15,6 +31,46 @@ const Login = () => {
return unsubscribe; return unsubscribe;
}, [navigation]); }, [navigation]);
const [userInfo, setUserInfo] = useState({
email: '',
password: '',
});
const { email, password } = userInfo;
const handleOnChangeText = (value, fieldName) => {
setUserInfo({ ...userInfo, [fieldName]: value });
// console.log(value, fieldName);
};
const [ error, setError ] = useState('');
const isValidForm = () => {
if (!isValidObjectField(userInfo)) {
return updateError('Required all fields!', setError);
}
if (!isValidEmail(email)){
return updateError('Invalid email !', setError);
}
if (!password.trim() || password.length < 8){
return updateError('Password is less than 8 characters !', setError);
}
return true;
}
const submitForm = () => {
if (isValidForm()){
()=> { navigation.navigate("Start")}
console.log(userInfo);
}
}
return( return(
<SafeAreaView> <SafeAreaView>
<ScrollView> <ScrollView>
...@@ -25,16 +81,19 @@ const Login = () => { ...@@ -25,16 +81,19 @@ const Login = () => {
</View> </View>
<View elevation={5} style={styles.main_container}> <View elevation={5} style={styles.main_container}>
<Text style={styles.main_title}>Sign In</Text> <Text style={styles.main_title}>Sign In</Text>
{error ? (<Text style={{color: 'red', fontSize: 18, textAlign: 'center'}}>{error}</Text>) : null}
<View style={styles.form_input}> <View style={styles.form_input}>
<TextInput style={styles.text_input} placeholder="Enter Email"></TextInput> <TextInput value={email} autoCapitalize="none" onChangeText={value => handleOnChangeText(value, 'email')} style={styles.text_input} placeholder="Enter Email"></TextInput>
</View> </View>
<View style={styles.form_input}> <View style={styles.form_input}>
<TextInput keyboardType="visible-password" style={styles.text_input} placeholder="Enter Password"></TextInput> <TextInput value={password} autoCapitalize="none" secureTextEntry onChangeText={value => handleOnChangeText(value, 'password')} keyboardType="visible-password" style={styles.text_input} placeholder="Enter Password"></TextInput>
</View> </View>
<View style={styles.form_input}> <View style={styles.form_input}>
<TouchableOpacity onPress={()=> { navigation.navigate("Start")}} style={styles.btn}> <TouchableOpacity onPress={submitForm} style={styles.btn}>
<Text style={styles.btn_text}> <Text style={styles.btn_text}>
Sign In Sign In
</Text> </Text>
...@@ -42,8 +101,8 @@ const Login = () => { ...@@ -42,8 +101,8 @@ const Login = () => {
</View> </View>
<View style={styles.text_if}> <View style={styles.text_if}>
<TouchableOpacity onPress={()=> { navigation.navigate("Register")}}> <TouchableOpacity onPress={()=> { navigation.navigate("Register")}}>
<Text style={styles.btn_text}> <Text style={styles.btn_text2}>
If you don't have account? Sign Up If you don't have account? Sign Up
</Text> </Text>
</TouchableOpacity> </TouchableOpacity>
...@@ -115,7 +174,12 @@ const styles = StyleSheet.create({ ...@@ -115,7 +174,12 @@ const styles = StyleSheet.create({
btn_text:{ btn_text:{
fontSize: 20, fontSize: 20,
fontWeight: 'bold', fontWeight: 'bold',
color:'#000000' color:'#000000',
},btn_text2:{
fontSize: 20,
fontWeight: 'bold',
color:'#000000',
marginTop:-10
}, },
text_if:{ text_if:{
fontSize: 20, fontSize: 20,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment