blob: dada09bef96609d55c3cf5164ec4bc1111ced53a [file] [log] [blame]
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +03001import {Directive, ElementRef} from '@angular/core';
2
3@Directive({
4 selector: '[patternInput]',
5 host: {
6 '(keypress)': 'onKeypress($event)'
7 }
8})
9export class InputPreventionPatternDirective{
10 inputElement : ElementRef;
11 constructor(el: ElementRef) {
12 this.inputElement = el;
13 }
14
15 onKeypress(event: KeyboardEvent) {
16 const pattern = new RegExp(this.inputElement.nativeElement.pattern);
17 if(pattern){
18 if(!pattern.test(event['key'])){
19 event.preventDefault();
20 }
21 }
22 return event;
23 }
24}