React Native - how to key number pad ONLY (without punctuations)

三世轮回 提交于 2020-01-13 10:18:14

问题


Is there a way to key a numeric keypad without punctuations?

<TextInput keyboardType='numeric' ..... />

if I use secureTextEntry={true}, it gets the right keyboard I want, but the values are replaced with *.

so, this is what im getting:

This is what I want:


回答1:


Did you try number-pad? both number-pad and numeric work for me on iOS.




回答2:


keyboardType={"number-pad"} Works on Android. Note: I am using FormInput from react-native-elements.




回答3:


I had the same problem.

I could solve doing something like this:

keyboardType={Device.isAndroid ? "numeric" : "number-pad"}

and then in a method call from onChangeText doing this:

const cleanNumber = number.replace(/[^0-9]/g, "");

this.setState({
  cleanNumber
});

and it the value prop of TextInput

value={this.state.cleanNumber}



回答4:


React native not provided keyboardType which remove punctuation from keyboard. You need to use regular expression with replace method to remove punctuation from text and set keyboardType = 'numeric'.

Regular Expression

/[- #*;,.<>{}[]\/]/gi

Example code

 onTextChanged(value) {
    // code to remove non-numeric characters from text
    this.setState({ number: value.replace(/[- #*;,.<>\{\}\[\]\\\/]/gi, '') });
  }

Please check snack link

https://snack.expo.io/@vishal7008/1e004c




回答5:


Please try this,

<TextInput 
     keyboardType = 'numeric'/>



来源:https://stackoverflow.com/questions/48798415/react-native-how-to-key-number-pad-only-without-punctuations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!