AnswerBun.com

ngx-mask Do not allow negative value for the currency input

Stack Overflow Asked by Ashot Aleqsanyan on November 28, 2020

I need to block the type of negative value and do not allow to type more numbers than 99,999,999.99 for my input.

Here is the code which I am using for the currency input.

<input mask="separator.2" thousandSeparator="," placeholder="Currency">

Any help will be appreciated.

Also here is the Stackblitz example.

https://stackblitz.com/edit/ngx-mask-currency-input?file=src/app/app.component.html

UPDATE

I found the answer to the second part of my question.
now the input looks like this

<input mask="separator.2" thousandSeparator="," separatorLimit="10000000"  placeholder="Currency">

Now just needs to be blocked the type of - character

2 Answers

Update on 01.09.2020

I have created the directive for blocking the type of negative(-) values.

Here is the directive example.

import { Directive, ElementRef, OnInit, OnDestroy } from "@angular/core";
import { fromEvent } from "rxjs/internal/observable/fromEvent";
import { Subscription } from "rxjs";

@Directive({
  // tslint:disable-next-line: directive-selector
  selector: "[onlyPositive]"
})
export class OnlyPositiveDirective implements OnInit, OnDestroy {
  subscriber$: Subscription;

  constructor(private element: ElementRef<HTMLInputElement>) {}

  ngOnInit() {
    const input = this.element.nativeElement;
    this.subscriber$ = fromEvent(input, 'input').subscribe(
      (event: KeyboardEvent) => {
        if (input.value.includes('-')) {
          input.value = input.value.replace(/-/g, '');
        }
      }
    );
  }

  ngOnDestroy() {
    this.subscriber$.unsubscribe();
  }
}

Usage example

<input onlyPositive mask="separator.2" thousandSeparator="," separatorLimit="99999999" [allowNegativeNumbers]="false" placeholder="Currency">

After Adams Advice I have changed the keypress event to the input event

DEMO: https://stackblitz.com/edit/ngx-mask-currency-input?file=src/app/app.component.html

Correct answer by Ashot Aleqsanyan on November 28, 2020

Demo you can solve with keypress event

<input (paste)="onPaste($event)" mask="separator.2" thousandSeparator=","  separatorLimit="10000000" [allowNegativeNumbers]="false" placeholder="Currency" class="form-control" (keypress)="isPositive($event)">

in component

  isPositive(event: any) { return event.key === '-' ? false : true; }

and block paste

 onPaste(event){
    event.preventDefault();
  }

Answered by pc_coder on November 28, 2020

Add your own answers!

Related Questions

Pygame doesn’t play mp3 files tagged with mutagen

1  Asked on January 14, 2021 by droptop

       

Average by group for ‘n’ number of rows per group – R

2  Asked on January 14, 2021 by codemaster

       

Non consecutive combinations of array elements in R

4  Asked on January 13, 2021 by guest2341

 

How to mutate complex object with nested array with SWR?

1  Asked on January 13, 2021 by geraltdiesocke

   

Why do I not see a text box to add a menu item to a menustrip

1  Asked on January 13, 2021 by justsomeguytn

   

Alembic. ModuleNotFoundError in env.py

2  Asked on January 13, 2021 by mroldsir

     

What type in c# is most like an expression in R and why?

1  Asked on January 12, 2021 by kirsten-greed

     

Difference between two loops

4  Asked on January 12, 2021 by mszros-zoltn

     

How can i do a request an external url?

0  Asked on January 12, 2021 by mikkylekyle

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir