blob: d00c52502c1609960905135bf243a60e657b3c18 [file] [log] [blame]
Ittay Stern6f900cc2018-08-29 17:01:32 +03001import {Directive, ElementRef, HostBinding, HostListener} from '@angular/core';
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +03002
3@Directive({
Ittay Stern6f900cc2018-08-29 17:01:32 +03004 selector: '[patternInput]'
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +03005})
6export class InputPreventionPatternDirective{
Ittay Stern6f900cc2018-08-29 17:01:32 +03007 @HostListener('keypress', ['$event']) onKeypress(event: KeyboardEvent) {
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +03008 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 }
Ittay Stern6f900cc2018-08-29 17:01:32 +030016
17 inputElement : ElementRef;
18 constructor(el: ElementRef) {
19 this.inputElement = el;
20 }
Sonsino, Ofir (os0695)ff76b5e2018-07-10 15:57:37 +030021}