You need to enable JavaScript to run this app. React Native函数组件中表单邮箱与8位以上密码校验实现方案
优惠活动
大模型
产品
解决方案
定价
更多

React Native函数组件中表单邮箱与8位以上密码校验实现方案

React Native函数式组件表单校验实现方案

核心修改点

  • 新增错误状态存储邮箱、密码的校验提示
  • 编写独立校验逻辑,可在提交时/实时输入时触发
  • 校验不通过时拦截提交逻辑,同时展示错误提示

完整实现代码

import { useState } from 'react';
import { View, TextInput, TouchableOpacity, Text, Alert } from 'react-native';
// 你原有依赖的Animatable、LinearGradient、useTheme、styles等保持原有引入即可

const SigninPage = ({navigation}) => {
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');
  // 新增错误状态
  const [emailError, setEmailError] = useState('');
  const [passwordError, setPasswordError] = useState('');
  const {colors} = useTheme();

  // 邮箱格式校验函数
  const validateEmail = (email) => {
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    return emailRegex.test(email);
  };

  // 密码长度校验函数
  const validatePassword = (password) => {
    return password.length >= 8;
  };

  // 提交前统一校验
  const handleSignIn = () => {
    // 先清空之前的错误
    setEmailError('');
    setPasswordError('');
    let isValid = true;

    if (!validateEmail(email)) {
      setEmailError('请输入正确的邮箱格式');
      isValid = false;
    }
    if (!validatePassword(password)) {
      setPasswordError('密码长度不能少于8位');
      isValid = false;
    }

    // 校验通过才执行登录逻辑
    if (isValid) {
      // 你原有的signIn逻辑写在这里
      Alert.alert('校验通过', '可以执行登录请求');
    }
  };

  // 可选:实时输入校验,输入过程中就提示错误
  const handleEmailChange = (text) => {
    setEmail(text);
    if (text && !validateEmail(text)) {
      setEmailError('请输入正确的邮箱格式');
    } else {
      setEmailError('');
    }
  };

  const handlePasswordChange = (text) => {
    setPassword(text);
    if (text && !validatePassword(text)) {
      setPasswordError('密码长度不能少于8位');
    } else {
      setPasswordError('');
    }
  };

  // 可选:判断是否允许提交,控制按钮禁用状态
  const isSubmitDisabled = !validateEmail(email) || !validatePassword(password);

  return(
    <View>
      <Animatable.View>
        <View style={styles.inputView}>
          <TextInput
            style={styles.TextInput}
            placeholder="E-mail"
            placeholderTextColor="white"
            onChangeText={handleEmailChange}
            value={email}
          />
          {/* 邮箱错误提示 */}
          {emailError ? <Text style={{color: 'red', marginTop: 4}}>{emailError}</Text> : null}
        </View>

        <View style={styles.inputView}>
          <TextInput
            style={styles.TextInput}
            placeholder="Password"
            placeholderTextColor="white"
            secureTextEntry={true}
            onChangeText={handlePasswordChange}
            value={password}
          />
          {/* 密码错误提示 */}
          {passwordError ? <Text style={{color: 'red', marginTop: 4}}>{passwordError}</Text> : null}
        </View>

        <TouchableOpacity
          disabled={isSubmitDisabled}
          style={{opacity: isSubmitDisabled ? 0.6 : 1}}
          onPress={handleSignIn}
        >
          <LinearGradient colors={['#730018', '#00085b']} style={styles.signIn}>
            <Text style={styles.textSign}>SIGN IN</Text>
          </LinearGradient>
        </TouchableOpacity>
        <Text>Forgot Your Password?</Text>
      </Animatable.View>
    </View>
  )
}

代码说明

  • 校验逻辑完全独立,和函数式组件的状态钩子适配,不需要类组件的this绑定逻辑
  • 实时校验、提交校验、按钮禁用三个能力都是可选配置,你可以根据需求选择保留或删除
  • 邮箱正则是通用格式校验规则,你可以根据业务需求调整正则的严格程度

内容的提问来源于stack exchange,提问作者Sanduni Fernando

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.20 08:54:01