1

I want to do a directive of can allow input enter only text and space, or integer, or float. but I get only false, in this 3 ways

 <div class="ui-g-10"  >
                <label for="Id">{{'Nome' | doisPontos}}</label><br>
                <div class="ui-inputgroup">
                    <input appFiltrarTipo type="text" formControlName="Nome" style="text-align: left; width: 450px;" id="Nome" appUppercase  [(ngModel)]="secaoForm.value.Nome" [value]="secaoForm.value.Nome" pInputText >
                </div>
            </div>

I pass how parameter I want preventDefault(), but in this 3 parameters the result is only false

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

let input = Input

@Directive({
    selector: '[appFiltrarTipo]'
})
export class FiltrarTipoDirective {

    private readonly REGEX = {
        text: '[a-zA-Z ]',
        integer: '[0-9]',
        float: '[0-9.]'
    }

    private readonly specialKeys = [ 'Backspace', 'Tab', 'End', 'Home', '-' ];

    @input() type: string;

    constructor(public el: ElementRef) {

        this.el.nativeElement.onkeypress = (evt) => {
            if(this.specialKeys.indexOf(evt.key) !== -1) return;
                let filter = new RegExp(this.REGEX[this.type])
           if(!filter.test(this.el.nativeElement.value)){
               event.preventDefault();
           }
           console.log(filter.test(this.el.nativeElement.value))


        };

    }
}
3
  • If you log the value of filter and this.el.nativeElement.value what is the output? Commented Sep 27, 2018 at 16:33
  • filter value: /[a-zA-Z ]/ el native value: Commented Sep 27, 2018 at 16:36
  • now i see, the filter is empty or null, why? Commented Sep 27, 2018 at 16:37

1 Answer 1

4

You're very close to the solution. To be more angular - leverage @HostListener. And as one of the comments suggested - you're checking the value of the element (input) on which the event happens. Instead you should check you pressed key value

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

@Directive({
  selector: '[appFiltrarTipo]'
})
export class AppFiltrarTipo {
  @Input() type: string;
  private readonly REGEX = {
    text: '[a-zA-Z ]',
    integer: '[0-9]',
    float: '[0-9.]'
  }
  private readonly specialKeys = ['Backspace', 'Tab', 'End', 'Home', '-'];

  @HostListener('keypress', ['$event'])
  onKeyPress(event: KeyboardEvent) {
    if (this.specialKeys.indexOf(event.key) !== -1) return;
    const filter = new RegExp(this.REGEX[this.type])

    if (!filter.test(event.key)) {
      event.preventDefault();
    }
  }
}

Full example here

Sign up to request clarification or add additional context in comments.

1 Comment

its work, thank you very much, i will study more about hotlistener

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.