Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 1 | import {Directive, ElementRef, HostBinding, HostListener} from '@angular/core'; |
Sonsino, Ofir (os0695) | ff76b5e | 2018-07-10 15:57:37 +0300 | [diff] [blame] | 2 | |
| 3 | @Directive({ |
Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 4 | selector: '[patternInput]' |
Sonsino, Ofir (os0695) | ff76b5e | 2018-07-10 15:57:37 +0300 | [diff] [blame] | 5 | }) |
| 6 | export class InputPreventionPatternDirective{ |
Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 7 | @HostListener('keypress', ['$event']) onKeypress(event: KeyboardEvent) { |
Sonsino, Ofir (os0695) | ff76b5e | 2018-07-10 15:57:37 +0300 | [diff] [blame] | 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 | } |
Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 16 | |
| 17 | inputElement : ElementRef; |
| 18 | constructor(el: ElementRef) { |
| 19 | this.inputElement = el; |
| 20 | } |
Sonsino, Ofir (os0695) | ff76b5e | 2018-07-10 15:57:37 +0300 | [diff] [blame] | 21 | } |