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

156
Vistas
only numbers regex JS works PC but not Android

I have a function that allows me only numbers by using a regular expression, the validation works fine in a desktop web browser but when I open the app on an android device with chrome, the validation breaks and allows typing any letters and symbols

I am using Angular and here is my function:

onlyNumber (event: KeyboardEvent) {
    let regex = /^\d*$/;
    const input = String.fromCharCode(event.key.charCodeAt(0));
    if (!regex.test(input)) {
      event.preventDefault();
    }
}

or number and letter only

 onlyNumberAndLetters = (event: KeyboardEvent) {
    let regex = /^[a-zA-Z0-9]*$/;
    const input = String.fromCharCode(event.key.charCodeAt(0));
    if (!regex.test(input)) {
      event.preventDefault();
    }
}

using

   <input type="text" class="form-control" minlength="8" maxlength="15"  formControlName="secretNumber"  (keypress)="onlyNumber($event)">

   <input type="text" class="form-control" minlength="5" maxlength="25"  formControlName="secretNumber2"  (keypress)="onlyNumberAndLetters($event)">

PD: I am using a text type field, because it was the easiest way to achieve the minlength and maxlength rule.

about 4 years ago · Juan Pablo Isaza
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