react-input-mask icon indicating copy to clipboard operation
react-input-mask copied to clipboard

Optional character

Open cezarsmpio opened this issue 9 years ago • 37 comments

There is a way to use a optional char?

Example: <MaskInput mask="(99) 9999-9999[9]" /> or <MaskInput mask="(99) 9999-9999{9}" /> or <MaskInput mask="(99) 9999-99999?" />

Thanks!

cezarsmpio avatar Jan 07 '16 11:01 cezarsmpio

Don't understand. How should optional char act?

sanniassin avatar Jan 10 '16 08:01 sanniassin

For example, in Brazil, some states have the phone number with one digit more. While some have 8 digits, others have 9 digits.

cezarsmpio avatar Jan 11 '16 10:01 cezarsmpio

Hi @sanniassin, any progress?

Thanks! :)

cezarsmpio avatar Feb 01 '16 16:02 cezarsmpio

@CezarLuiz0 Pretty busy right now, so this project temporarily abandoned :)

sanniassin avatar Feb 01 '16 18:02 sanniassin

Can I fork this project and send pull requests with new features?

Thanks!

cezarsmpio avatar Feb 01 '16 18:02 cezarsmpio

Sure, but i will not be able to review and test PR for a while.

sanniassin avatar Feb 01 '16 18:02 sanniassin

Hi guys! Is there any progress on this feature?

deser avatar Feb 08 '16 07:02 deser

I can do it for 100$ )

dapi avatar Apr 03 '16 17:04 dapi

Ok, please do it then)

deser avatar Apr 03 '16 17:04 deser

Have done. Just use this configuration:

<Input 
  mask="(99) 9999-99999?"
  formatChars={
    "9": "[0-9]",
    "?": "[0-9]"
  }
 />

dapi avatar Apr 03 '16 18:04 dapi

Thank you!)

deser avatar Apr 03 '16 18:04 deser

Guys, is this issue solved?

eliseumds avatar Apr 27 '16 21:04 eliseumds

Not yet :/

cezarsmpio avatar Apr 27 '16 21:04 cezarsmpio

Does not last solution fit?

dapi avatar May 09 '16 17:05 dapi

Hey guys, how is this issue?

leaopedro avatar Jul 18 '16 18:07 leaopedro

Does anyone has a solution to have a formatted input for currencies, like USD 1,000?

mquandalle avatar Aug 17 '16 17:08 mquandalle

There is Intl standard. You can use react Intl as well

deser avatar Aug 17 '16 20:08 deser

Sure, but the difficult part is to have an <input> that auto-format the number as the user types it. Like autoNumeric in the jQuery world.

mquandalle avatar Aug 17 '16 21:08 mquandalle

@mquandalle I use http://numeraljs.com/ for this solving this task

sashashakun avatar Oct 06 '16 12:10 sashashakun

Hi guys,

I had the same problem with the different formatted phone numbers. I solved using a mask like this:

<InputElement
    mask="(99) 9999tt999?" 
    formatChars={{"9": "[0-9]", "t": "[0-9\-]", "?": "[0-9 ]"}} 
    maskChar={null} />

Now both formats are accepted. As there is no placeholder character, it looks fine for the user.

alymenbr avatar Dec 05 '16 21:12 alymenbr

Great solution, @alymenbr! But i don't think that the issue is solved, because all this solutions are just "work arounds" for a feature that does no exists.

kaueburiti avatar Jan 23 '17 17:01 kaueburiti

Hi guys, I have a issue with creating mask for zipcode US, as some 5 digits, and some have 9 digits, I try to make a "work around" like this : mask="99999?????" formatChars={{ 9: '[0-9]', '?': '[-\0-9]' }} but not working correctly, is there any suggestion ?

dkouk avatar Apr 23 '18 10:04 dkouk

For controlled inputs:

handlerChangeBrazilianPhone = (ev) => {
  const brazilianPhone = ev.target.value.replace(/[^0-9]+/g, '')
  this.setState({ brazilianPhone })
}
...
mask={this.state.brazilianPhone.length <= 10 ? '(99) 9999-9999?' : '(99) 99999-9999'}
formatChars={{ 9: '[0-9]', '?': '[0-9 ]' }}
onChange={this.handlerChangeBrazilianPhone}
value={this.state.brazilianPhone}

andredovale avatar Apr 24 '18 21:04 andredovale

@dkouk Could you take a look at the master branch? It has new beforeChange property and example with your case.

sanniassin avatar Apr 27 '18 15:04 sanniassin

is there a way to achieve this inputmask("9{2,3}/9{2,3}").inputmask("option", {tabThrough: true}) using input mask in react? I am looking to use a mask where i can enter 2 or 3 numbers before / if i enter 2 numbers ( e.g 12_/___)and press tab it should take me to the second portion after /.

Any help is highly appreciated.

nagapriyanka93 avatar May 01 '18 19:05 nagapriyanka93

hi i want react mask for values 1 to 100 i am using "99%" mask but its not taking the 100 value i want to include the 100 max value also

Abhishek-Palapi avatar Oct 17 '18 05:10 Abhishek-Palapi

import React, { useState } from 'react';
import InputMask from 'react-input-mask';
import { trim, size } from 'lodash';

const PhoneInput = ({ className, ...props }) => {
  const [mask, setMask] = useState('(99) 99999-9999');

  return (
    <InputMask
      {...props}
      mask={mask}
      onBlur={e => {
        if (size(trim(e.target.value, '_')) === 14) {
          setMask('(99) 9999-9999');
        }
      }}
      onFocus={e => {
        if (size(trim(e.target.value, '_')) === 14) {
          setMask('(99) 99999-9999');
        }
      }}
    >
      {inputProps => (<input {...inputProps} type="tel" />)}
    </InputMask>
  );
};

fiuzagr avatar Oct 30 '19 23:10 fiuzagr

to whoever falls here searching for an react-number-format solution

          const s = values.phone
            .split(" ")
            .join("")
            .split(")")
            .join("")
            .split("(")
            .join("")
            .split("-")
            .join("")
            .split("_")
            .join("")

format={s.length < 11 ? "(##) ####-#####" : "(##) #####-####"}
mask={s.length < 10 ? "_" : ""}

cescoferraro avatar Aug 20 '20 01:08 cescoferraro

I did like this

with: react-input-mask: ^2.0.4

import React from "react";
import InputMask, { InputState } from "react-input-mask";
import { TextField } from "@material-ui/core";

function PhoneMask() {
  const beforeMaskedValueChange = (newState: InputState) => {
    let { value } = newState;

    const newValue = value.replace(/\D/g, "");
    if (newValue.length === 11) {
      value = newValue.replace(/^(\d{2})(\d{5})(\d{4})$/, "($1) $2-$3");
    }

    return {
      ...newState,
      value
    };
  };

  return (
    <InputMask mask="(99) 9999-99999" maskChar={null} beforeMaskedValueChange={beforeMaskedValueChange}>
      {(props: any) => <TextField {...props} fullWidth label="Phone" name="phone" />}
    </InputMask>
  );
};

hugosbg avatar Aug 21 '20 03:08 hugosbg

perfect, @hugosbg 👌 thank you.

marlosirapuan avatar Sep 24 '20 20:09 marlosirapuan