bhrott / react-native-masked-text

A pure javascript masked text and input text component for React-Native.
MIT License
1.61k stars 249 forks source link

Add CPF/CNPJ Mask #242

Closed guilhermemoretto12 closed 3 years ago

guilhermemoretto12 commented 3 years ago

It would be great if a CNPJ/CPF Mask were added, something like the Cel Phone Mask

lucastryhorn commented 3 years ago

<TextInput type="custom" keyboardType="numeric" options={{ mask: state.typeFavoredCompany === 'cpf' ? '999.999.999-99*' : '99.999.999/9999-99', validator: (value) => { return validateCnpj(value) || validateCPF(value); }, getRawValue: (value) => { return (value || '').replace(/\.|\/|-/g, ''); }, }} placeholder="CNPJ ou CPF" value={state.favoredCompany} onChangeText={(text) => setState({ ...state, favoredCompany: text, typeFavoredCompany: text.length > 14 ? 'cnpj' : 'cpf', }) } />

CaioQuirinoMedeiros commented 3 years ago

I have just what you need: https://github.com/CaioQuirinoMedeiros/react-native-mask-input

const [cpfCnpj, setCpfCnpj] = React.useState("")

return (
  <MaskInput
      mask={Masks.BRL_CPF_CNPJ}
      value={cpfCnpj}
      onChangeText={(masked, unmasked) => {
          setCpfCnpj(masked)
      }}
      keyboardType="numeric"
      placeholder="CPF/CNPJ"
   />
)