简体   繁体   English

如何在React Native中单击提交按钮来验证电子邮件和密码?

[英]How to validate email and password on click of submit button in React native?

I'm new to react-native and by following the docs I have created a simple login screen. 我是React-native的新手,通过阅读文档,我创建了一个简单的登录屏幕。 Now I'm not getting how to validate input fields on click of submit button. 现在,我不了解如何在单击“提交”按钮时验证输入字段。 Below is the code for login screen. 以下是登录屏幕的代码。

import React, { Component } from 'react';
import {
  AppRegistry,
  Text,
  Image,
  View,
  Button,
  StyleSheet,
  TextInput,
  Linking,
  Alert
} from 'react-native';

class LoginPage extends Component {
//On click of submit button alert will appear
  _onSubmit() {
    Alert.alert('Button has been pressed!');
  }
  render() {
    return (
      <View style={styles.containerView}>
        <Text style={styles.loginText} >
          Username or Email
        </Text>
        <TextInput style={styles.inputFields}
          placeholder="e.g: abc@example.com" />
        <Text style={styles.loginText}>
          Password
        </Text>
        <TextInput style={styles.inputFields}
          placeholder="Password"
          secureTextEntry={true} />
        <Text style={{color: 'blue', marginBottom: 10}}
          onPress={() => Linking.openURL('https://www.google.co.in')}>
          Forgot Password?
        </Text>
        <Button
            onPress={this._onSubmit}
            title="Login"
            style={styles.loginButton}
            accessibilityLabel="Ok, Great!"
          />
      </View>
    );
  }
};

//Stylesheet for input fields
const styles= StyleSheet.create({
  containerView: {
    flex: 1,
    justifyContent: 'center',
    backgroundColor: '#ffebcd',
    borderStyle: 'solid',
    borderColor: '#000000'
  },
  loginText: {
    fontSize: 20,
    marginBottom: 10
  },
  inputFields: {
    fontSize: 20,
    borderStyle: 'solid',
    borderColor: '#000000',
    borderRadius: 30,
    marginBottom: 10
  },
  loginButton: {
    backgroundColor: '#34A853'
  }
});

AppRegistry.registerComponent('AwesomeProject', () => LoginPage);

basically working with forms in react native is somewhat challenging. 基本上在react native中使用表单是有点挑战的。 If you want some mature solution I would suggest to take a look at tcomb-form-native 如果您想要一些成熟的解决方案,我建议您看一下tcomb-form-native

If you want to keep things simple, then you have to store references to your inputs and then (for example on onSubmitEditing ) you can get entered value and validate it by yourself. 如果您想保持简单,则必须存储对输入的引用,然后(例如onSubmitEditing )可以获取输入的值并自行验证。

import React, { Component } from 'react';
import {
  AppRegistry,
  Text,
  Image,
  View,
  Button,
  StyleSheet,
  TextInput,
  Linking,
  Alert
} from 'react-native';

class LoginPage extends Component {
state = {
  username: null,
  password: null
}
//On click of submit button alert will appear
  _onSubmit() {
    const { username, password } = this.state;
    Alert.alert('Button has been pressed!');
  }
  render() {
    return (
      <View style={styles.containerView}>
        <Text style={styles.loginText} >
          Username or Email
        </Text>
        <TextInput style={styles.inputFields}
          onChangeText={value => this.setState({ username: value })}
          placeholder="e.g: abc@example.com" />
        <Text style={styles.loginText}>
          Password
        </Text>
        <TextInput style={styles.inputFields}
          onChangeText={value => this.setState({ password: value })}
          placeholder="Password"
          secureTextEntry={true} />
        <Text style={{color: 'blue', marginBottom: 10}}
          onPress={() => Linking.openURL('https://www.google.co.in')}>
          Forgot Password?
        </Text>
        <Button
            onPress={this._onSubmit}
            title="Login"
            style={styles.loginButton}
            accessibilityLabel="Ok, Great!"
          />
      </View>
    );
  }
};

//Stylesheet for input fields
const styles= StyleSheet.create({
  containerView: {
    flex: 1,
    justifyContent: 'center',
    backgroundColor: '#ffebcd',
    borderStyle: 'solid',
    borderColor: '#000000'
  },
  loginText: {
    fontSize: 20,
    marginBottom: 10
  },
  inputFields: {
    fontSize: 20,
    borderStyle: 'solid',
    borderColor: '#000000',
    borderRadius: 30,
    marginBottom: 10
  },
  loginButton: {
    backgroundColor: '#34A853'
  }
});

AppRegistry.registerComponent('AwesomeProject', () => LoginPage);

您可以使用this.state.username进行验证

<TextField label={'Username'} onChangeText={(username)=>this.setState({username})} value={this.state.username} />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React-Native 传递用户 email 和密码提交按钮 - React-Native Passing users email and password to submit button 如何在 React Native 中验证单次提交单击时的所有输入 - How to Validate All Input On Single Submit Click In React Native React Native 如何验证用户名和密码 - React native How to validate username and password 在 React Native 中,当用户在 TextInput 中写入时,如何避免用户点击提交按钮两次? - In React Native, how can I avoid having a user click a submit button twice, when they are writing in a TextInput? 当文本输入和提交按钮都在 React Native 中的不同 js 文件中时,如何在单击提交时进行空检查并导航到新屏幕? - How to null check and navigate to new screen on click submit when textinput and submit button both are in different js files in react native? React Native:如何在提交时验证用户名和密码 - React native: How to validate username and password while submitting React Native按钮单击 - React Native button click 如何在 React Native 中点击按钮打开 DatePicker - how to open DatePicker on Button Click in React Native 如何在本机反应中以编程方式单击按钮 - how to click a button programmatically in react native 如何在React Native中单击按钮以打开React Native模式? - How to open a react native modal on button click in react native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM