【问题标题】:react native form Email and Password validation in react functional component在反应功能组件中反应本机表单电子邮件和密码验证
【发布时间】:2021-08-09 06:05:27
【问题描述】:

我创建了一个简单的表单。我需要知道如何验证至少 8 个字符的电子邮件和密码。许多教程都是为类组件设计的。但我无法用功能组件处理它们。请帮帮我

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>
)

}

【问题讨论】:

    标签: react-native validation react-functional-component


    【解决方案1】:

    亲爱的,你快完成了!

    看,我稍微修改了你的代码。

    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>
    )
    

    }

    我希望你也需要同样的东西。

    【讨论】:

    • 谢谢德鲁夫!您能否解释一下并给出如何在此处包含电子邮件验证器?因为我无法意识到这一点
    • 非常感谢,Dhruv!你让我的任务更轻松了!
    猜你喜欢
    • 2021-08-18
    • 1970-01-01
    • 2019-06-26
    • 2021-10-13
    • 2021-02-16
    • 2019-01-16
    • 2020-04-03
    • 2022-09-25
    • 2021-11-23
    相关资源
    最近更新 更多