我只想允许文本输入,不允许在文本中复制粘贴
我已经做到了数字键盘应该打开,但我只想允许在此输入字段期间输入数字
<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 })
}
}
关于javascript - 我如何在 react 中只允许数字输入,例如,如果有人尝试复制粘贴文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57366049/