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

85
Vistas
Format input currency jquery

I'm having some problems creating the mask for the currency field.

1- I need to limit UP TO 18 characters (counting the '.' and the ',') Ex: 999.999.999.999,99

2 - I need you to put more than one '.' (currently only one)

3 - I need this not to happen:

enter image description here

Because when I type it adds the '.' right then before I type in one more number.

My code:

function onlyNumberAmount(input) {
    var v = input.value;
    v = v + '';
    v = parseInt(v.replace(/[\D]+/g, ''));
    v = v + '';
    v = v.replace(/([0-9]{2})$/g, ",$1");
    v = v.replace(/([0-9]{3}),([0-9]{2}$)/g, ".$1,$2");
    input.value = v;
    if(v == 'NaN') input.value = '';
}
<input type="text" class="form-control" id="mP0"  onkeyup="onlyNumberAmount(this)">

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

0

The following seems to meet your requirements.

function onlyNumberAmount(input) {
  let v = input.value.replace(/\D+/g, '');
  if (v.length > 14) v = v.slice(0, 14);
  input.value =
    v.replace(/(\d)(\d\d)$/, "$1,$2")
     .replace(/(^\d{1,3}|\d{3})(?=(?:\d{3})+(?:,|$))/g, '$1.');
}
<input type="text" class="form-control" id="mP0" onkeyup="onlyNumberAmount(this)">

\d is short for [0-9] as I'm sure you know.

A positive lookahead (?=(?:\d{3})+(?:,|$)) is used to ensure the correct placement of the .

14 is the maximum number of digits that can appear in the string.

Further explanation on request.

about 4 years ago · Juan Pablo Isaza Denunciar

0

If, test if the field is already valid

const moneyTest = /(\d{1,3}\.)?(\d{1,3}\.)?(\d{1,3})?(.\d{1,2})?/

if (!moneyTest.test(field)) {

Then, if it's not, separate into the number fields

var digits = /d/g.match(field); // might be field.match(/d/g)

Now you have to see if there was a decimal place in the original number

var dot = field.lastIndexOf(".");

And then you should be good to reconstruct the number.

Question: What happens if there are two dots?

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