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

@Directive({
  selector: '[patternInput]',
  host: {
    '(keypress)': 'onKeypress($event)'
  }
})
export class InputPreventionPatternDirective{
  inputElement : ElementRef;
  constructor(el: ElementRef) {
    this.inputElement = el;
  }

  onKeypress(event: KeyboardEvent) {
    const pattern = new RegExp(this.inputElement.nativeElement.pattern);
    if(pattern){
      if(!pattern.test(event['key'])){
        event.preventDefault();
      }
    }
    return event;
  }
}