Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
La entrada Html acepta coma o punto como lugar decimal

Me gustaría agregar una validación para que una entrada html acepte solo números, una coma o un decimal. Esto es para precios basados en la UE donde un usuario desea ingresar un precio en el formato de 3,22 o 3,22. Ambos deberían estar permitidos. Pero el usuario no puede ingresar una combinación de decimal o coma. Me gustaría manejar esto usando expresiones regulares, ya que me parece más adecuado. Esto es lo que pude encontrar.

 <input class="form-control price_field" type="text" id="article_selling_price" name="article_selling_price">

Un código JS que encontré para manejar solo comas

 $(".price_field").on("keyup", checkKey); function checkKey() { var clean = this.value.replace(/[^0-9,]/g, "").replace(/(,.*?),(.*,)?/, "$1"); if (clean !== this.value) this.value = clean; }

¿Hay alguna manera de que pueda usar algo similar para lograr mi requisito? No estoy tan familiarizado con expresiones regulares

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Me las arreglé para que funcionara de una manera diferente al verificar charCode y también una función de tecla para reemplazar el punto con coma.

 <input class="form-control price_field" onkeypress="return isNumberKey(this, event);" type="text" id="price_search" name="price_search"> function isNumberKey(txt, evt) { var charCode = (evt.which) ? evt.which : evt.keyCode; if (charCode == 44) { //check if previously there was a decimal if (txt.value.indexOf('.') > 0) { return false; } //Check if the text already contains the , character if (txt.value.indexOf(',') === -1) { return true; } else { return false; } } else if(charCode == 46){ //check if previously there was a comma if (txt.value.indexOf(',') > 0) { return false; } if (txt.value.indexOf('.') === -1) { return true; } else { return false; } } else { if (charCode > 31 && (charCode < 48 || charCode > 57)) return false; } return true; } $(".price_field").on("keyup", checkKey); function checkKey() { if (this.value.indexOf('.') > 0) { this.value = this.value.replace(".", ","); } }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!