[英]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.