import { Directive, ElementRef, HostListener } from '@angular/core';

@Directive({
    selector: '[AadharCard]'
})
export class AadharCardDirective {
    private regexp = /^[2-9]{1}[0-9]{3}\\s[0-9]{4}\\s[0-9]{4}$/;
   
    constructor(private el: ElementRef) { }

    @HostListener('keydown', ['$event'])
    onKeyDown(event: KeyboardEvent) {
        
        if (["Backspace", "Tab", "End", "Home"].indexOf(event.key) !== -1)
            return;

        let current: string = this.el.nativeElement.value;
        let next: string = current.concat(event.key);
        console.log(this.regexp.test(next));
        
        if (next && !this.regexp.test(next))
            event.preventDefault();
    }
}