Answers for "custom directive to only allow regex pattern in input field angular"

0

custom directive to only allow regex pattern in input field angular

gfhtrdhtdetrdtr676878798989898)(_+_+
Posted by: Guest on August-25-2021
0

angular numbers only directive

import { Directive, ElementRef, HostListener, Input } from '@angular/core';
import { NgControl } from '@angular/forms';

@Directive({
  selector: 'input[type=number], input[numbersOnly]'
})
export class NumbersOnlyInputDirective {

  constructor(private elRef: ElementRef) { }

  @HostListener('input', ['$event']) onInputChange(event) {
    const initalValue = this.elRef.nativeElement.value;
    this.elRef.nativeElement.value = initalValue.replace(/[^0-9]*/g, '');
    if ( initalValue !== this.elRef.nativeElement.value) {
      event.stopPropagation();
    }
  }

}
Posted by: Guest on May-04-2020

Code answers related to "custom directive to only allow regex pattern in input field angular"

Code answers related to "TypeScript"

Browse Popular Code Answers by Language