javascript - 例如,如果有人试图复制粘贴文本,我如何只允许数字输入做出反应
问题描述
我只想允许文本输入,不允许文本中的复制粘贴
我已经让数字键盘应该打开,但我只想在这个输入字段期间允许数字
<TextInput
underlineColorAndroid='transparent'
style={styles.TextInputStyle}
placeholder='0.00'
keyboardType={'numeric'}
value={this.state.shippingCharge}
onChangeText={(shippingCharge) => this.setState({ shippingCharge })}
/>
解决方案
尝试Regex
对输入使用验证(例如最大长度为 100 个数字的数字):
<TextInput
underlineColorAndroid='transparent'
style={styles.TextInputStyle}
placeholder='0.00'
keyboardType={'numeric'}
value={this.state.shippingCharge}
onChangeText={this.onChangeTextInput}
/>
onChangeTextInput = (text) => {
const numericRegex = /^([0-9]{1,100})+$/
if(numericRegex.test(text)) {
this.setState({ shippingCharge: text })
}
}
推荐阅读
- python-2.7 - IOError: [Errno 13] Permission denied: '/home/arjun/iKy/backend/factories/apikeys.json' in parrot os
- html - 如何防止 Bootstrap 类重叠?
- oracle - 如何将所有插入脚本组合到一个函数中
- makefile - 我们可以有多个具有相同目标的规则吗?
- opencv - 在 RaspberryPi 中同时运行两个网络摄像头的问题
- python - 'itertools._grouper' 对象没有属性 'user'
- r - R:如何为数据帧的每一行采样不同的列?
- firebase - Firebase 推送通知始终以空消息的形式到达
- java - 在 Collectors.joining 中避免空白字符串的前缀后缀
- c++ - 如何返回派生自抽象类的类的对象?