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

186
Vistas
Check if a string with comma and period is a proper decimal number Javascript and extract numeric value

I am trying to validate my input field by checking if a string is a proper number with comma and period and if yes get the value

I tried two functions both not working as expected:

function isNumeric(str) {
    console.log(parseFloat(str.replace(/,/g,'')))  
    return !isNaN(str) && // use type coercion to parse the _entirety_ of the string (`parseFloat` alone does not do this)...
           !isNaN(parseFloat(str)) // ...and ensure strings of whitespace fail
  }


  const isValidNumber = (str) => {
      console.log(parseFloat(str.replace(/,/g,'')))
    //   return /^[0-9,.]*$/.test(str);
      return /^\d{0,4}(?:[.,]\d{1,3})?$/.test(str)
    //   return /^[0-9,]?.[0-9]/.test(str);
    // return /([0-9,.]+)$/.test(str)
  }

the test cases I prepared

console.log(isNumeric("2"))//true
console.log(isNumeric("2,00,000"))//true
console.log(isNumeric("2,7,5,.34"))//false
console.log(isNumeric("2,450,0000.22.22"))//false
console.log(isNumeric("2q,3,4"))//false

But it's not working as intended, I need a regex to fix this. Also need to check if parseFloat(str.replace(/,/g,'')) is the right way to extract value

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

0

Here is a general regex pattern for matching a number with proper thousands separator and an optional decimal component:

\d{1,3}(?:,\d{3})*(?:\.\d+)?

function isNumeric(input) {
    return /^\d{1,3}(?:,\d{3})*(?:\.\d+)?$/.test(input);
}

console.log(isNumeric("2"));                 // true
console.log(isNumeric("2,000,000"));         // true
console.log(isNumeric("3.14159"));           // true
console.log(isNumeric("2,7,5,.34"));         // false
console.log(isNumeric("2,450,0000.22.22"));  // false
console.log(isNumeric("2q,3,4"));            // false

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