Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native text input mask

We're developing program on React Native, and I have a question. How to make a text input mask for ZIP code and mobile phone number via React Native?

Here's a sample:

like image 500
Denis Spro Avatar asked Jul 27 '16 08:07

Denis Spro


People also ask

How do I mask text input in react native?

import TextInputMask from 'react-native-text-input-mask'; ... <TextInputMask onChangeText={(formatted, extracted) => { console. log(formatted) // +1 (123) 456-78-90 console. log(extracted) // 1234567890 }} mask={"+1 ([000]) [000] [00] [00]"} /> ...

How do I make an input mask?

Customize input masks from the field property settingIn the Navigation Pane, right-click the object and click Design View on the shortcut menu. Click the field where you want to create the custom input mask. In the Field Properties area, click the Input Mask text box, and then type your custom mask.

How do I disable paste in input field react native?

Use caretHidden={true} if you want to disable all operation like Cut Paste Copy. It will also hide your cursor as well.


Video Answer


1 Answers

Check out this library. https://github.com/wix/react-native-ui-lib

(or directly this: https://github.com/wix/react-native-ui-lib#masked-input)

It allows you to render custom masked input in any format you want.

You can use it as follow: (this is an example for a phone number)

import {MaskedInput} from 'react-native-ui-lib'

// in your render...
<MaskedInput
  renderMaskedText={this.renderMaskedInput}
  caretHidden
  keyboardType={'numeric'}
/>

renderMaskedInput(value) {

  return (
    <View>
      <Text>
      {value.substr(0, 3)} - {value.substr(3, 10)}
      <Text>
    <View>
  );
}
like image 121
Ethan Sharabi Avatar answered Oct 30 '22 01:10

Ethan Sharabi