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

227
Views
Jquery para permitir números negativos y positivos en el campo de texto y el signo negativo debe tener 0 índices

Necesito validar un campo de texto para aceptar solo números negativos o positivos y la indexación también es importante. esto es lo que he intentado hasta ahora.

este es el código HTML

 <input type="text" class="negativeaccept" >

aqui esta jquery

 $(document).on('keypress','.negativeaccept', function(event) { if (((event.which != 46 || (event.which == 46 && $(this).val() == '')) || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) { event.preventDefault(); } });

este código solo acepta un número positivo con un decimal. Quiero un signo negativo (-) de los índices 0.

pruebo este código y funciona (event.which != 45 || $(this).val().indexOf('-') != -1) && pero el signo negativo (-) ingresa cualquier índice. ¿Puedes probar esto mejor que esta solución?

 $(document).on('keypress','.negativeaccept', function(event) { if (((event.which != 46 || (event.which == 46 && $(this).val() == '')) || $(this).val().indexOf('.') != -1) && (event.which != 45 || $(this).val().indexOf('-') != -1) && (event.which < 48 || event.which > 57)) { event.preventDefault(); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="text" class="negativeaccept">

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