Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Currency mask without need to type decimal separator #733

Open
pedrollmatias opened this issue Apr 24, 2020 · 3 comments
Open

Currency mask without need to type decimal separator #733

pedrollmatias opened this issue Apr 24, 2020 · 3 comments
Labels

Comments

@pedrollmatias
Copy link

pedrollmatias commented Apr 24, 2020

Hi,

I'm trying to mask a value as brazilian currency. I want a mask which the user don't need to type the decimal separator (in my case, a comma) and the typed number is formatted to BRL currency automatically. For example:

Typed value > Masked value > Control value:
1 > R$ 0,01 > 0.01
10 > R$ 0,10 > 0.10
100 > R$ 1,00 > 1
123456 > R$ 1.234,56 > 1234.56

Actually, the pattern to get the control value according to typed value is a division by 100:

1 : 100 = 0.01
10 : 100 = 0.10
...

The closest I got was

<input mask="separator.2" [thousandSeparator]="'.'" [decimalMarker]="','" prefix="R$ " />

but user must type the comma to have the decimal part.

Is possible to get this type of mask only with ngx-mask and without write some extra code?

@juninhocruzg3
Copy link

I've added the code below to the checkInputPrecision method in the mask-applier.service.ts file. I got the expected result for this proposed issue. However, the cursor in the input field is not stayed, and it's moved to before the decimal separator.
I've used the "split" method because I don't have extensive knowledge of pattern method.

private checkInputPrecision = (
    inputValue: string,
    precision: number,
    decimalMarker: IConfig['decimalMarker'],
    beginByDecimal = false
  ): string => {
    if (precision < Infinity) {
      const precisionRegEx = new RegExp(this._charToRegExpExpression(decimalMarker) + `\\d{${precision}}.*$`);
      const precisionMatch = inputValue.match(precisionRegEx);

      if (beginByDecimal) {
        if (!precisionMatch) {
          const splited = inputValue.split(decimalMarker);
          const decimalLength = splited.length === 2 ? splited[1].length : 0;
          const diff = precision - decimalLength;
          if (diff > 0) {
            const integer = splited[0].length > diff ? splited[0].substring(0, splited[0].length - diff) : '';
            const toChange =
              splited[0].length > diff ? splited[0].substring(splited[0].length - diff, splited[0].length) : splited[0];
            const end = splited.length === 2 ? splited[1] : '';
            let decimal = toChange + end;
            decimal =
              precision - decimal.length > 0
                ? Array(precision - decimal.length)
                    .fill(0)
                    .join('') +
                  '' +
                  decimal
                : decimal;
            inputValue = integer + decimalMarker + decimal;
          }
        } else {
          const splited = inputValue.split(decimalMarker);
          const decimalLength = splited.length === 2 ? splited[1].length : 0;
          const diff = decimalLength - precision;
          if (diff > 0) {
            let integer = splited[0];
            const toChange = splited[1].substring(0, diff);
            const end = splited[1].substring(diff, splited[1].length);
            integer = integer + toChange;
            inputValue = integer + decimalMarker + end;
          }
        }
      } else {
        if (precisionMatch && precisionMatch[0].length - 1 > precision) {
          const diff = precisionMatch[0].length - 1 - precision;
          inputValue = inputValue.substring(0, inputValue.length - diff);
        }
        if (precision === 0 && inputValue.endsWith(decimalMarker)) {
          inputValue = inputValue.substring(0, inputValue.length - 1);
        }
      }
    }
    return inputValue;
  };

@d10n4t4n
Copy link

Pretty sad we have to use another projects just because ngx-mask does not care to solve this high requested problem.

@duwejeferson
Copy link

Would be very appreciated feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants