Sonsino, Ofir (os0695) | ff76b5e | 2018-07-10 15:57:37 +0300 | [diff] [blame] | 1 | import {Directive, ElementRef} from '@angular/core'; |
| 2 | |
| 3 | @Directive({ |
| 4 | selector: '[patternInput]', |
| 5 | host: { |
| 6 | '(keypress)': 'onKeypress($event)' |
| 7 | } |
| 8 | }) |
| 9 | export 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 | } |