I have read similar questions and no solutions seem to be working for this. I have a Angular material input box. The input should receive only numbers and a decimal dot. All other characters should be identified and removed on keypress removal.
This is my code:
In TS file and html:
allownumbersonly(inputVal) {
inputVal = inputVal.replace(/^(\d+(?:[\.\,]\d{2})?)$/, "");
//inputVal = inputVal.replace(/[^0-9 ]+(\.)?/g, "");
this.myForm.controls['inNum'].setValue(inputVal);
}
<mat-form-field><input id="inputNumber" matInput (keyup)="allownumbersonly($event.target.value)" placeholder="enter a number"
formControlName="inNum"></mat-form-field>
Please help. Thanks in advance.