merge from ecomp a88f0072 - Modern UI
[vid.git] / vid-webpack-master / src / app / shared / directives / inputPrevention / inputPreventionPattern.directive.ts
1 import {Directive, ElementRef, HostBinding, HostListener} from '@angular/core';
2
3 @Directive({
4   selector: '[patternInput]'
5 })
6 export class InputPreventionPatternDirective{
7   @HostListener('keypress', ['$event']) onKeypress(event: KeyboardEvent) {
8     const pattern = new RegExp(this.inputElement.nativeElement.pattern);
9     if(pattern){
10       if(!pattern.test(event['key'])){
11         event.preventDefault();
12       }
13     }
14     return event;
15   }
16
17   inputElement : ElementRef;
18   constructor(el: ElementRef) {
19     this.inputElement = el;
20   }
21 }