I want input the digit numbers in the textbox only. Either a number or a number with two decimals.
For example, 12345
or 1234.56
.
I found some code online but I can't locate the resource now. That one only works for a number but fails on the two decimals. I want to modify the code. Here it is.
currencyChars = new RegExp('[\.,]', 'g'); // we're going to remove commas and dots
constructor(public element: ElementRef, public renderer: Renderer2, private decimalPipe:
DecimalPipe) {}
ngOnInit() {
this.format(this.element.nativeElement.value);
}
@HostListener('input', ["$event.target.value"]) onInput(e: string) {
this.format(e);
}
format(val: string) {
const numberFormat = parseInt(String(val).replace(this.currencyChars, ''));
const usdollar = this.decimalPipe.transform(numberFormat, '1.0', 'en-US');
this.render.setProperty(this.element.nativeElement, 'value', usdollar);
}
I guess that I have to change the format method. Please help.