0

I am using an functionality in which when I start typing in an Input box It will add the commas to the no and which is working fine but my issue is its not allowing decimals so how to allow decimals also with commas ?

Below code I am using to achieve commas thing.

<input type="text" matInput [(ngModel)]="budget" placeholder="budget" autocomplete="off" (keydown)="numberCheck($event)" (keyup)="CommaFormatted($event)">


 CommaFormatted(event) {
    if(event.which >= 37 && event.which <= 40) return;
   
    if (event.target.value) {
      event.target.value = event.target.value.replace(/\D/g, "")
       .replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    }
  }
   
   numberCheck (args) {
   if (args.key === 'e' || args.key === '+' || args.key === '-') {
     return false;
   } else {
     return true;
   }
  }

So how to allow decimals also in this input box. Any help will be appreciated.

Thanks in advance.

  • With the pattern attribute you can specify the allowed format with a regular expression in a HTML5 compatible way. Here you could specify that the comma character is allowed and a helpful feedback message if the pattern fails. https://stackoverflow.com/questions/15303940/how-to-handle-floats-and-decimal-separators-with-html5-input-type-number does the above link answer your question? –  Apr 06 '22 at 07:42
  • no pattern is not working here. – Namit Singh Apr 06 '22 at 09:40

0 Answers0