Im trying to write a directive that limit user to input numeric only characters in the input text control.
@Directive({
  selector: '[numericControl]'
})
export class NumericControlDirective {
    contructor(
        private el: ElementRef,
    ) {}
    @HostListener('input', ['$event'])
    onInput(e: any) {
        if (e.which < 48 || e.which > 57) {
            e.preventDefault();
        }
    }
}
Usage
<input type="text" placeholder="Volume" numericControl />
But it doesn't work, Anyone came across this issue?
Use Keyboard event type - keydown or keypress:
@HostListener('keydown', ['$event'])
onInput(e: any) {
  if (e.which < 48 || e.which > 57) {
      e.preventDefault();
  }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With