Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

205
Vistas
Replace symbols not working on keypress event

I have input where I need to delete non number symbols

Here is html of component

  <input
  type="text"
  tabindex="0"
  class="search__input form-control-md + {{ class }}"
  [value]="config.value"
  [required]="config.required"
  [attr.maxlength]="config.maxLength"
  [attr.minLength]="config.minLength"
  (focusout)="onFocusOut($event)"
  (input)="onValueChange($event)"
  (keypress)="onValueChange($event)"
  (keyup.enter)="onEnter($event)"
  #inputElem
/>

Here is component

    export class TextFieldComponent implements OnInit, ControlValueAccessor {
  @Input() config: FormBase<string>;
  @Input() form: FormGroup;
  @Output() onChanged = new EventEmitter<boolean>();
  @Input() class: string;
  configControl = new FormControl();
  @Input() set value(value: string) {
    this._value = value;
  }
  get value(): string {
    return this._value;
  }

  private _value: string;

  constructor() {}

  ngOnInit() {}

  onFocusOut(event) {
    this.onValueChange(event);
    this.onChanged.emit(true);
  }

  onEnter(event) {
    this.onValueChange(event);
    this.onChanged.emit(true);
  }

  onValueChange(event) {
    this.changeValue(event.target.value);
  }

  writeValue(value) {
    this.value = value;
  }

  changeValue(value) {
    if (this.value === value) {
      return;
    }
    this.value = value;
    let result;
    switch (this.config.isNumber) {
      case true:
        result = value != null ? value.toString().replace(/[^0-9]/g, "") : null;
        console.log(result);
        this.onChange(result);
        break;
      default:
        this.onChange(value);
    }
  }

  onChange: any = () => {};

  onTouched: any = () => {};

  registerOnChange(fn) {
    this.onChange = fn;
  }

  registerOnTouched(fn) {
    this.onTouched = fn;
  }
}

My problem, that on focusout for example, this method changeValue is working well and it delete non number symbols, but on keypress event I see that on console I have replaced value, but on input, I still see letters. How I can solve this issue?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You need to understand what exactly keypress is doing here. The keypress event is fired when a key that produces a character value is pressed down and examples of keys that produce a character value are alphabetic, numeric, and punctuation keys.

Examples of keys that don't produce a character value are modifier keys such as Alt, Shift, Ctrl, or Meta.

In the above case the keypress event won't invoke and will only be called when the character values are produced.

You should use keyUp or keyDown event to track things more effectively.

Note: check this link for the difference b/w keyup, keydown and keypress.

about 4 years ago · Juan Pablo Isaza Denunciar

0

In your component access input element using ViewChild

@ViewChild('inputElem') inputEl: ElementRef;

Then to set result after replace

result = value != null ? value.toString().replace(/[^0-9]/g, '') : null;

// update input value
this.inputEl.nativeElement.value = result;

console.log(result);

Demo

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda