validation - 如何在本机反应中验证文本输入以获取最少字符
问题描述
我有一个文本输入供用户发送消息。如果用户输入少于 10 个字符,我希望弹出错误消息。
<TextInput
style={styles.textArea}
underlineColorAndroid="transparent"
placeholder="Ask a question or comment"
placeholderTextColor="grey"
numberOfLines={1}
textAlignVertical= 'top'
multiline={true}
/>
解决方案
您可以在TouchableOpactiy和TochablableHighlight中的按钮 onPress 上进行验证。并且还在onSubmitEditing道具中使用blurOnSubmit。单击按钮时建议使用代码。
onSubmit = () => {
if (this.state.name.trim().length < 8) {
Alert.alert('Alert', 'Password must be minimum 8 characters');
return;
}
//Do your stuff if condition meet.
}
render() {
return (
<View style={{ flex: 1 }}>
<TextInput
style={{ height: 40, borderColor: 'gray', borderWidth: 1 }}
onChangeText={(text) => this.setState({ password: text })}
maxLength={16}
secureTextEntry={true}
value={this.state.password}
/>
<TouchableOpacity onPress={() => this.onSubmit()}>
<Text style={styles.submit}>Submit</Text>
</TouchableOpacity>
</View>
)
}
推荐阅读
- android - JCenter 为 Play 服务抛出 409
- c - 如何使用 Kernel Panic log 识别内存损坏的原因?
- python - 用 Python 自动化无聊的东西 - 逗号代码
- vmware-clarity - 清晰的 UI 子导航
- mysql - Mysql CURDATE() 在 12:00AM-1:30AM 不工作
- c++ - 在数组中比较/搜索多个整数的最佳解决方案
- range - 创建箱线图,指定每列 seaborn 的特定值范围
- python - Python 3.7:大数据文件比较的性能调优
- twitter-bootstrap-3 - 如何在 Laravel 中从 Bootstrap 4 降级到 3
- jmeter - 响应时间随测试持续时间的变化