如何在 React Native 中验证手机号码是否接受唯一的数值。
<TextInput
ref='mobileNo'
keyboardType="numeric"
style={[styles.textInput, { width: '100%' }]}
placeholder='Enter mobile number'
onChangeText={(value) => this.handleChange('mobileNo', value)} />
我使用了 keyboardType="numeric"
但它也接受特殊字符,所以我想接受唯一的数值。
最佳答案
您需要检查输入的字符串是否为数字。检查以下代码:
<TextInput
ref='mobileNo'
keyboardType="numeric"
style={[styles.textInput, { width: '100%' }]}
placeholder='Enter mobile number'
onChangeText={(value) =>
let num = value.replace(".", '');
if(isNaN(num)){
// Its not a number
}else{
this.handleChange('mobileNo', num)}
}
/>
关于javascript - react native : mobile number validation accept numeric value only,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50441460/