Code Monkey home page Code Monkey logo

ng2-currency-mask's Introduction

ng2-currency-mask

A very simple currency mask directive for Angular that allows using a number attribute with the ngModel. In other words, the model is a number, and not a string with a mask. It was tested in Angular version 13.

Example App

https://stackblitz.com/edit/angular-ivy-bpn8by

Getting Started

Installing and Importing

Install the package by command:

    npm install ng2-currency-mask --save

Import the module

import { CurrencyMaskModule } from "ng2-currency-mask";

@NgModule({
    imports: [
        ...
        CurrencyMaskModule
    ],
    declarations: [...],
    providers: [...]
})
export class AppModule {}

Using

<input currencyMask [(ngModel)]="value" />
  • ngModel An attribute of type number. If is displayed '$ 25.63', the attribute will be '25.63'.

Options

You can set options as follows:

<!-- example for pt-BR money -->
<input currencyMask [(ngModel)]="value" [options]="{ prefix: 'R$ ', thousands: '.', decimal: ',' }" />

Available options:

  • align - Text alignment in input. (default: right)
  • allowNegative - If true can input negative values. (default: true)
  • decimal - Separator of decimals (default: '.')
  • precision - Number of decimal places (default: 2)
  • prefix - Money prefix (default: '$ ')
  • suffix - Money suffix (default: '')
  • thousands - Separator of thousands (default: ',')

You can also set options globally...

import { CurrencyMaskConfig, CurrencyMaskModule, CURRENCY_MASK_CONFIG } from 'ng2-currency-mask';

export const CustomCurrencyMaskConfig: CurrencyMaskConfig = {
    align: "right",
    allowNegative: true,
    decimal: ",",
    precision: 2,
    prefix: "R$ ",
    suffix: "",
    thousands: "."
};

@NgModule({
    imports: [
        ...
        CurrencyMaskModule
    ],
    declarations: [...],
    providers: [
        { provide: CURRENCY_MASK_CONFIG, useValue: CustomCurrencyMaskConfig }
    ],
    bootstrap: [AppComponent]
})
export class AppModule {}

Validation

This directive also provides built-in validation for minimum and maximum values. If the attributes 'min' and / or 'max' are set, the Angular CSS class 'ng-invalid' will be added to the input to indicate an invalid value.

<input currencyMask [(ngModel)]="value" min="-10.50" max="100.75" />

ng2-currency-mask's People

Contributors

cesarrew avatar dsosedov-vmw avatar felipex1x avatar ivofernandes avatar jasonbrandt42 avatar leonardograselalmeida avatar musatov avatar thomaswdean avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ng2-currency-mask's Issues

[question] global options

Hi @cesarrew, thanks for this, saved my day!

How can we set global options for an entire app instead of going with the below config for each and every input?

[options]="{ prefix: 'R$ ', thousands: '.', decimal: ','}"

Minus appears before prefix on negative values

Hi! First of all, thanks for this module, it saved me lots of time!
Here the issue:
When I use a custom prefix, let's say %, when the value of the input is a negative number it puts the minus before the prefix instead of after (e.g. -%0.3 instead of %-0.3).

Thanks again!

After i install it gave me error in console InjectionToken is not a constructor

zone.js:522 Unhandled Promise rejection: core_1.InjectionToken is not a constructor ; Zone: angular ; Task: Promise.then ; Value: TypeError: core_1.InjectionToken is not a constructor
at Object.1054 (currency-mask.config.js:4)
at webpack_require (bootstrap d02c331fe602561fc89b:52)
at Object.1010 (currency-mask.directive.js:5)
at webpack_require (bootstrap d02c331fe602561fc89b:52)
at Object.1053 (index.js:6)
at webpack_require (bootstrap d02c331fe602561fc89b:52)
at Object.957 (0.chunk.js:5136)
at webpack_require (bootstrap d02c331fe602561fc89b:52)
at src async:15
at ZoneDelegate.webpackJsonp.951.ZoneDelegate.invoke (zone.js:334)
at Object.onInvoke (ng_zone.js:273)
at ZoneDelegate.webpackJsonp.951.ZoneDelegate.invoke (zone.js:333)
at Zone.webpackJsonp.951.Zone.run (zone.js:126)
at zone.js:713
at ZoneDelegate.webpackJsonp.951.ZoneDelegate.invokeTask (zone.js:367) TypeError: core_1.InjectionToken is not a constructor
at Object.1054 (http://localhost:4200/0.chunk.js:4379:32)
at webpack_require (http://localhost:4200/inline.bundle.js:53:30)
at Object.1010 (http://localhost:4200/0.chunk.js:2030:30)
at webpack_require (http://localhost:4200/inline.bundle.js:53:30)
at Object.1053 (http://localhost:4200/0.chunk.js:4366:10)
at webpack_require (http://localhost:4200/inline.bundle.js:53:30)
at Object.957 (http://localhost:4200/0.chunk.js:5136:76)
at webpack_require (http://localhost:4200/inline.bundle.js:53:30)
at http://localhost:4200/main.bundle.js:11818:10
at ZoneDelegate.webpackJsonp.951.ZoneDelegate.invoke (http://localhost:4200/polyfills.bundle.js:2907:26)
at Object.onInvoke (http://localhost:4200/vendor.bundle.js:35548:37)
at ZoneDelegate.webpackJsonp.951.ZoneDelegate.invoke (http://localhost:4200/polyfills.bundle.js:2906:32)
at Zone.webpackJsonp.951.Zone.run (http://localhost:4200/polyfills.bundle.js:2699:43)
at http://localhost:4200/polyfills.bundle.js:3286:57
at ZoneDelegate.webpackJsonp.951.ZoneDelegate.invokeTask (http://localhost:4200/polyfills.bundle.js:2940:31)

KeyValueDiffer is not generic

I get this error when compiling:

/node_modules/ng2-currency-mask/src/currency-mask.directive.d.ts

Typescript Error
Type 'KeyValueDiffer' is not generic.

inputHandler: InputHandler;
keyValueDiffer: KeyValueDiffer<any, any>;
optionsTemplate: {

Type over partly selected Input

Hi @cesarrew, thanks for the great plugin,

This is probably related to Issue #15. When we select part of the current input to replace it nothing happens on key Inputs. We are forced to delete the respective parts without selecting them and enter the new values.

For us this is relevant when the user wants to make small changes to his input but doesnt want to reenter his whole input.

When user types in a alphabet, i get value back as 0 for my model

Hi,

I have basically 2 issues.

My field is mandatory, so i watch for errors on field and set as error.

  1. When user clears the filed value remains as 0.0
  2. When user types in alphabets it return value as 0, which effects my validation as on screen we don't show any value, but error message pop ups because of validation.

Type over Input behavior is disabled

If I select all or part of an existing value and try to replace it by typing "over" the selected text, I'm unable to do so (including using the DELETE key).

TypeError: Cannot set property 'textAlign' of undefined

Hello - I receiving the following error: "TypeError: Cannot set property 'textAlign' of undefined". I only see this error after adding the 'currencyMask' directive to my input box. I'm not sure where to start debugging this... please advise. Thanks!

Can't type any number into the field on Android 6.0.1

As the title stated, can't type in any number on Android 6.0.1. Not sure if my pattern is wrong. It works in iOS, Chrome, Firefox and Safari, just not in Android (Chrome).

<input currencyMask [(ngModel)]="amount" name="amount" [options]="{prefix: ''}" pattern="^[1-9]|([0-9]{1,10}\.\d{1,2})$" type="text" class="form-control" placeholder="Amount" required>

Update Prefix

Its possible change the prefix with a select input ?

Text-mask with ReactiveFormsModule, does not work.

I am using the directive ng2-currency-mask, which is working perfectly, in my form I am using validation with ReactiveFormsModule, in the fields that I use the ng2-currency-mask validation does not work. I've tried it in every way without success.

Someone got it?
Do you have an example so you can tell me?

Thank you.

Binding to (change) event

Hi! This plugin has been super useful, thank you! I was wondering if it would be possible to let us bind to the (change) event of the input field? (Not sure if I'm using the right terminology here...) Thanks in advance!

If it will take a while to get to, I can try to do a pull request, although it would probably take me a while to get to the level of understanding I need to make it happen.

Error in version 2.x

In ionic 2, install version 2.0.0 and ocurred this error in compiling

rollup failed: 'CurrencyMaskModule' is not exported by node_modules\ng2-currency-mask\index.js

Am I doing something wrong?

"npm install [email protected] --save" and import module in app.module.ts

import { CurrencyMaskModule } from ng2-currency-mask

Avoiding suffix value on ngChangeModel

When a use some suffix value attribute in the suffix parameter it is applied to the value of ngModel variable incorrectly changing my ngModel variable, could I do something to avoid this ?

This is an example of my code:

<input class="form-control default" currencyMask [(ngModel)]="realtySimulation.entry" (ngModelChange)="onChangeSimulation('entry',$event)" [options]="{ prefix: 'R$ ', thousands: '.', decimal: ',', suffix: ' ('+somevalue+'%)' }"/>

Max not working

Hi

I wanted to use your mask with the max attribute in order to allow no number greater than 100.
How can I do that. The original max attribute of the input element is not working.

Thanks and cheers
Tobias

InjectionToken error after install

After installing and importing the module, the error appears while compiling:

typescript: ...ento/MyApp/app/node_modules/ng2-currency-mask/src/currency-mask.config.d.ts, line: 1
            Module '"D:/Desenvolvimento/MyApp/app/node_modules/@angular/core/index"' has no exported member
            'InjectionToken'.

       L1:  import { InjectionToken } from "@angular/core";
       L2:  export interface CurrencyMaskConfig {

[15:53:12]  typescript: ...o/MyApp/app/node_modules/ng2-currency-mask/src/currency-mask.directive.d.ts, line: 12
            Type 'KeyValueDiffer' is not generic.

      L11:  inputHandler: InputHandler;
      L12:  keyValueDiffer: KeyValueDiffer<any, any>;
      L13:  optionsTemplate: {

Options from locale configuration

Could be possible your directive take default options from locale provided in app module:

@NgModule({
providers: [
{ provide: LOCALE_ID, useValue: "es-ES" },
//otherProviders...
]
})

Left align issue

I have added the options on my custom input component in angular 2 and it does not align left while other options like prefix etc., works well. Any idea?
[options]="{ align:'left' }"

Options

Hi,
Great directive!
Could there be no way to change management options once and for all?
Without having to repeat it for each element.
After all, when you have chosen the display options for a currency, I think it's normal that most of the elements use the same.
Thanks in advance.

After using this i cannot use change event using angular2

<input type="text" class="form-control"currencyMask id="perUnitPrice" required [(ngModel)]="model.perUnitPrice" name="perUnitPrice" (change)="CalculateTotalAmount(addPurchaseOrderForm.value)">

change event is not called after change value

Issue when no decimal digits are provided

When I enter 1111.00 in my text box it saves are 1111.00 in database but when I get data back to the text box with Currency mask it shows as $ 11.11 instead of $ 1111.00 ( In this case I don't have anything in my decimal places except zeros). But when I have some thing in decimal places like 1244.44 it saves and shows in the text box as $1244.44. Any idea?

Validation states such as 'touched' not applied

The touched property is not set to true after clicking into and out of the input element.
I'm using validation messages on the page which rely on these properties.
Any chance you could add support for this?
Thanks. Rich.

On clearing field the input defaults to '0'

On clearing an input field I would like the text to be blank rather than default to '0' Ideally, I would like to be able to configure this in the options settings. At the moment even when I delete the 0, if still says zero.

Add precision to non-decimal value

Currently there is precision for decimals but not for integer values. Normally precision refers to the integer values and SCALE refers to the decimal values. Please add this to the options attributes. Thanks!
:) Mike

Doesn't seem to work on Android

I'm using this component (thank you!) but it doesn't seem to work on Chrome on Android. (Android 6)

When the user types the mask is not overwritten, and the validation fails regardless of what the user types.

I'm using AoT compilation if that makes any difference.

Flexible precision

Currently, it's not possible to have the user enter between 0 and 2 decimals. We're using this in a finance context, where often we specify bigger numbers like EUR 25.000. In these cases, it's not user friendly to require the user to type 2500000.

Would it be possible to add some flexibility in this regard?

Mouse Focus Left Point

How can i set mouse focus to left of decimal point? when i press Tab and focus to input i want to focus to 0{mouse_focus_here}.00
For now mouse focus 0.00{here}. i do not want to focus like that.

Prefix after number

Hi, is it possible to put text after currency number, e.g. $ 1,000,000.00 USD ?

Doesn't work when input type is set

When had my input type set to 'number' my input was blank and it printed this error when I tried to write something:

ERROR TypeError: Cannot read property 'createRange' of undefined

Once I remove the type from the input it works.

This was my input:
<input id="icon_prefix" type="number" currencyMask [(ngModel)]="value" [options]="{prefix: '', thousands: '.', decimal: ',', align: 'left', allowNegative: 'false' }" (change)="onUpdateAmount()" min="1">

Type 'KeyValueDiffer' is not generic.

Hi Guys,

How do I deal with this error?
ap-angular-portal/node_modules/ng2-currency-mask/src/currency-mask.directive.d.ts (10,21): Type 'KeyValueDiffer' is not generic.

@angular/cli: 1.0.0
node: 7.7.4
os: darwin x64
@angular/common: 2.4.10
@angular/compiler: 2.4.10
@angular/core: 2.4.10
@angular/forms: 2.4.10
@angular/http: 2.4.10
@angular/material: 2.0.0-beta.2
@angular/platform-browser: 2.4.10
@angular/platform-browser-dynamic: 2.4.10
@angular/router: 3.4.10
@angular/cli: 1.0.0
@angular/compiler-cli: 2.4.10

Cannot find type definition file for 'core-js'.

I use this library in Angular 2 project.

but It output these error message.

[default] C:\Develop\ng2-primeng-sample\node_modules\ng2-currency-mask\src\currency-mask.directive.d.ts:1:0 
Cannot find type definition file for 'core-js'.
[default] C:\Develop\ng2-primeng-samle\node_modules\ng2-currency-mask\src\input.handler.d.ts:1:0 
Cannot find type definition file for 'core-js'.

And When I install core-js by npm i --save types@core-js, there are new errors as followings

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:21:13
Duplicate identifier 'PropertyKey'.

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:85:4
All declarations of 'name' must have identical modifiers.

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:145:4
Subsequent variable declarations must have the same type.  Variable '[Symbol.unscopables]' must be of type '{ copyWithin: boolean;

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:262:4
All declarations of 'flags' must have identical modifiers.

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:276:4
All declarations of 'EPSILON' must have identical modifiers.

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:311:4
All declarations of 'MAX_SAFE_INTEGER' must have identical modifiers.

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:318:4
All declarations of 'MIN_SAFE_INTEGER' must have identical modifiers.

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:457:4
Subsequent variable declarations must have the same type.  Variable '[Symbol.toStringTag]' must be of type '"Symbol"', but here has

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:457:4
All declarations of '[Symbol.toStringTag]' must have identical modifiers.

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:464:4
All declarations of 'prototype' must have identical modifiers.

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:492:4
All declarations of 'hasInstance' must have identical modifiers.

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:498:4
All declarations of 'isConcatSpreadable' must have identical modifiers.

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:504:4
All declarations of 'iterator' must have identical modifiers.

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:510:4
All declarations of 'match' must have identical modifiers.

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:516:4
All declarations of 'replace' must have identical modifiers.

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:522:4
All declarations of 'search' must have identical modifiers.

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:528:4
All declarations of 'species' must have identical modifiers.

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:534:4
All declarations of 'split' must have identical modifiers.

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:540:4
All declarations of 'toPrimitive' must have identical modifiers.

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:546:4
All declarations of 'toStringTag' must have identical modifiers.

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:552:4
All declarations of 'unscopables' must have identical modifiers.

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:609:4
Subsequent variable declarations must have the same type.  Variable '[Symbol.toStringTag]' must be of type '"Math"', but here has t

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:609:4
All declarations of '[Symbol.toStringTag]' must have identical modifiers.

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:613:4
Subsequent variable declarations must have the same type.  Variable '[Symbol.toStringTag]' must be of type '"JSON"', but here has t

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:613:4
All declarations of '[Symbol.toStringTag]' must have identical modifiers.

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:628:4
All declarations of 'size' must have identical modifiers.

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:634:4
All declarations of 'prototype' must have identical modifiers.

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:645:4
All declarations of 'size' must have identical modifiers.

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:651:4
All declarations of 'prototype' must have identical modifiers.

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:666:4
All declarations of 'prototype' must have identical modifiers.

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:680:4
All declarations of 'prototype' must have identical modifiers.

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:692:4
All declarations of 'value' must have identical modifiers.

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\@types\core-js\index.d.ts:804:4
All declarations of 'prototype' must have identical modifiers.

ERROR in [default] C:\Develop\ng2-primeng-omine\node_modules\typescript\lib\lib.es2015.core.d.ts:17:13

so I tried to delete /// <reference types="core-js" /> from currency-mask.directive.d.ts and input.handler.d.ts.
After this solution, Error isn't appeared, but I think that this solution is not good and will be cause of other error.
but there is no error now.
Is this a correct solution ?

I use angular 2.1.1, PrimeNG 1.0.0 and current of ng2-currency-mask.

Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.