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

114
Vistas
Regex Javascript block Input

i've just build a javascript function with a regex control that allows 3 numbers and one dot.

function restrictNumber(e) {
  var newValue = this.value.replace(new RegExp(/^(?!^(?:\d{1,3}|\d(?:\d?\.\d?|\.\d{2}))$).*/, 'gm'), "");
  this.value = newValue;
}

$('.decimal').on('input', restrictNumber);
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<input class="decimal" type="text">

The specs of the regex are:

  • max. 3 numbers
  • only one or no dot
  • dot can be anywhere

Here is a regex demo for it: https://regex101.com/r/3Ru1O3/3/

i tried to "block" the input when a char is put in that isn't fitting. But when i try to test it deletes my hole string.

How can i change that behaviour that i just can't set new numbers put the string isnt vanishing.

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

0

You didn't say much about your actual requirements, as in, if the decimal is required at a certain place value, etc.. but based on what your existing regex does, it could definitely be shortened.

function restrictNumber(e) {
  var newValue = this.value.replace(/[^\d\.]/, "").substr(0,4);
  this.value = newValue;
}

$('.decimal').on('input', restrictNumber);
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<input class="decimal" type="text">

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