0

I have this:

  <input style="text-align: right;font-size: 12px;" class='input' (keyup.enter)="sumTotal($event)" type="text"
      [ngModel]="field.value" (focusin)="focusin()" (focusout)="format()" (keyup.enter)="format()"
      (ngModelChange)="onlyNumbers($event);field.value = $event;sumTotal($event);" [disabled]="disabled">

In my ts i have this: I want numbers and special characters.

  onlyNumbers(letter) { if (letter.toUpperCase() !== letter.toLowerCase()) { return; } else { this.field.value = letter; } }

But it still can enter letters. Any suggestion?

Liam
  • 27,717
  • 28
  • 128
  • 190
None
  • 8,817
  • 26
  • 96
  • 171
  • 1
    use `type="number"` – Crocsx Sep 20 '19 at 07:24
  • i cant number because it can be enter 3,00 2.00 , 2.000,00 and so on – None Sep 20 '19 at 07:25
  • Then explain your question better, what do you mean when you say `I want numbers and special characters.` what special character you want to allow, which not. and why not , . – Crocsx Sep 20 '19 at 07:25
  • i write that already in my question – None Sep 20 '19 at 07:26
  • Possible duplicate of [Angular2 - Input Field To Accept Only Numbers](https://stackoverflow.com/questions/41465542/angular2-input-field-to-accept-only-numbers) – Liam Sep 20 '19 at 07:28

2 Answers2

2

Using event.preventDefault() by checking if isNaN returns false and if its not a . or , could be a solution for you:

function numbersonly(e){
  if(isNaN(e.key) && e.key !== '.' && e.key !== ',') e.preventDefault();
}
<input type="text" onkeypress="numbersonly(event)" />
jsadev.net
  • 2,800
  • 1
  • 16
  • 28
0

You can try this way

 onkeypress="return (event.charCode == 8 && event.charCode == 0) ? null : event.charCode >= 48 && event.charCode <= 57"

other way set pattern

 pattern="[0-9]"
Bansi29
  • 1,053
  • 6
  • 24