20

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:

enter image description here

This is what I want:

enter image description here

JMP
  • 4,417
  • 17
  • 30
  • 41
Raheel Hasan
  • 5,753
  • 4
  • 39
  • 70

6 Answers6

18

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}
Miguel Cardenas
  • 1,215
  • 9
  • 5
4

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

Jithesh Kt
  • 2,023
  • 6
  • 32
  • 48
3

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/numeric-keyboard

Vishal Dhanotiya
  • 2,512
  • 1
  • 13
  • 32
2

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

Karim Varela
  • 7,562
  • 10
  • 53
  • 78
1

Android doesn't support keyboardType without punctuations A visual guide to the React Native TextInput keyboardType prop values

Vasyl Nahuliak
  • 1,912
  • 2
  • 14
  • 32
1

For Android in my case I prevent set any special char with Miguel Cardenas example.

Here what I did:

const inputRef = useRef(null);
const setRefValue = v => {
      const clean = v.replace(/[^0-9]/g, '');
      inputRef.current.value = clean;
      inputRef.current.setNativeProps({ text: clean });
    }
  };

<TextInput
   ref={inputRef}
   keyboardType={numeric}
   onChangeText={setRefValue}
/>

Every time type in TextInput the constant setRefValue will clean special characters.

  • But fraction of second value display then clean, it's not good. How to make input like phonepe and Google pay after qr scan, when we type hyphen doesn't happen like that way we want in react native. – gausmohammad Jan 21 '23 at 15:44