Javascript 如何限制react native中textfield的退格事件
我想限制用户删除phonenumber字段中的第一个字符。我已经实现了如下所示的Javascript 如何限制react native中textfield的退格事件,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我想限制用户删除phonenumber字段中的第一个字符。我已经实现了如下所示的onKeyPress事件处理程序,但它仍然没有限制删除文本 onKeyPress={({ nativeEvent }) => { if (nativeEvent.key === "Backspace" && this.props.input.value.charAt(0) === '+') { return false } }} 召唤 在哪里 o
onKeyPress
事件处理程序,但它仍然没有限制删除文本
onKeyPress={({ nativeEvent }) => {
if (nativeEvent.key === "Backspace" && this.props.input.value.charAt(0) === '+') {
return false
}
}}
召唤
在哪里
onKeyPress(event) {}
在函数中
不要使用nativeEvent。使用onKeyDown而不是onKeyPress,如果条件已通过,请执行e.preventDefault()
函数你的函数(e){
如果(e.code==='Backspace'&&e.target.value.charAt(0)=='+'&&e.target.selectionStart如果您不希望他们更改它,您是否考虑过不将其包含在字段中?
e、 g.(香草HTML)
4
其中“4”是纯文本,无法更改。不起作用。nativeEvent.preventDefault()
不是函数错误。如果用户用另一个字符更改您的第一个字符,它将不起作用。请尝试从输入中删除此字符,稍后再处理它。我不知道,但preventDefault()
在react native中生成错误。是否可以尝试使用setNativeProps
onKeyPress(event) {}
4<input type="tel"/>