summaryrefslogtreecommitdiffstats
path: root/vid-webpack-master/src/app/shared/directives/inputPrevention/inputPreventionPattern.directive.ts
blob: d00c52502c1609960905135bf243a60e657b3c18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import {Directive, ElementRef, HostBinding, HostListener} from '@angular/core';

@Directive({
  selector: '[patternInput]'
})
export class InputPreventionPatternDirective{
  @HostListener('keypress', ['$event']) onKeypress(event: KeyboardEvent) {
    const pattern = new RegExp(this.inputElement.nativeElement.pattern);
    if(pattern){
      if(!pattern.test(event['key'])){
        event.preventDefault();
      }
    }
    return event;
  }

  inputElement : ElementRef;
  constructor(el: ElementRef) {
    this.inputElement = el;
  }
}