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

时间:2017-03-22 13:38:09

标签: react-native

我是反应原生的新手,并且按照我创建了一个简单登录屏幕的文档。现在我没有得到如何在点击提交按钮时验证输入字段。以下是登录屏幕的代码。

$('#search_btn').click(function(){
  $('#sortable_table').removeAttr('data-role');
});

3 个答案:

答案 0 :(得分:0)

基本上使用本机反应表单有点挑战性。如果您想要一些成熟的解决方案,我建议您查看tcomb-form-native

如果您想保持简单,那么您必须存储对输入的引用,然后(例如在onSubmitEditing上)您可以获得输入值并自行验证。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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