简体   繁体   中英

How to control the mask of an input?

I have an API that returns the mask of an input based on the country code. What I still need to do is a function that dynamically formats the input as the user types.

For example, the user selects the code +55 and I get that the input mask will be (##)####-####, how can I format the input from the mask?

I tried downloading some libraries but most use the mask in the form of a regex, and that's not how I get it from my api.

If you take this library for example https://github.com/CaioQuirinoMedeiros/react-native-mask-input You can create mask for it like:

import { formatWithMask } from 'react-native-mask-input'

const dataFromBackend = '(##)####-####'
const phoneMask = dataFromBackend.split('').map(item => item === '#' ? /\d/ : item)

const { masked, unmasked, obfuscated } = formatWithMask({
  text: '71680345',
  mask: phoneMask,
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM